I have implemented push notification in my app.it was working fine in ios6. when i updated my app to ios7 i have some issues in push notifcations.
when the app launches below delegate method gets called everytime either notification are enabled or not.
that's fine with ios6.
but in ios7 if notifications are disabled by user this method not gets called
as a result of this i get device token null.
help me on this.
thanks for any help
(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *strDeviceToken = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
strDeviceToken = [strDeviceToken stringByReplacingOccurrencesOfString:#" " withString:#""];
self.mydeviceToken=strDeviceToken;
[appShareData setStrDeviceToken:strDeviceToken];
[[NSNotificationCenter defaultCenter] postNotificationName:K_GETTHEDEVICETOKEN object:nil userInfo:nil];
}
Related
Currently one of my php developer provided me push notification API for iOS devices
The problem is : If i run that api with respective parameter in any Browser(Chrome/Safari/Firefox and etc..) i am getting notification on foreground of iOS device. But not in iOS app(Xcode) itself
In my app i used code like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Register for Push Notitications, if running on iOS 8
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}
#pragma mark
#pragma mark -- Push Notification Delegate Methods
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings: (UIUserNotificationSettings *)notificationSettings{
//register to receive notifications
[application registerForRemoteNotifications];
}
-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
// Prepare the Device Token for Registration (remove spaces and < >)
devToken = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:#"<"withString:#""]
stringByReplacingOccurrencesOfString:#">" withString:#""]
stringByReplacingOccurrencesOfString: #" " withString: #""];
NSLog(#"My token is: %#", devToken);
// My token is: cd2887c4093569b3eed142320f21a81e521e486cf5c40467390901d3a191398b
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:deviceToken forKey:#"deviceToken"];
}
-(void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{
NSLog(#"Failed to get token, error: %#", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(#"%s..userInfo=%#",__FUNCTION__,userInfo);
}
I am getting response:(in didReceiveRemoteNotification)
{
aps = {
alert = "You're login successfully";
sound = default;
};
}
This message is not showing on Status bar(top of the screen). Is there any issue in iOS side (or) PHP side
If the issue is in iOS side--> How can i do this
Here is my Testing Push notification API:
https://shopgt.com/mobile1/iphone/register.php?device_type=2&email=sukhpal#anaad.net®Id=4d1d9067cc1382ecb8b0532831cce7fc8eb6fc388a6139060cd84712407a0ae5
Can you please help me out regarding this issue
You need to customize the view for showing Banner of Push Notification while the app in Foreground. You can use JCNotificationBannerPresenter. Follow the sample code using below link.
https://github.com/jcoleman/JCNotificationBannerPresenter
#import "JCNotificationCenter.h"
#import "JCNotificationBannerPresenterSmokeStyle.h"
- (void) application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)notification {
NSString* title = #"Push Notification";
NSDictionary* aps = [notification objectForKey:#"aps"];
NSString* alert = [aps objectForKey:#"alert"];
[JCNotificationCenter
enqueueNotificationWithTitle:title
message:alert
tapHandler:^{
NSLog(#"Received tap on notification banner!");
}];
}
Hope it Helps you..!
There is no issue, this is default behaviour.
The banner that appears at the top of the screen when you are on the Home screen does not appear when you are inside the app.
You need to get the app to do something with the notification yourself, inside didReceiveRemoteNotification. This could be showing an alert, adding a badge to the tab bar, etc. but banners only show when you are outside of the app.
I am facing a problem in iOS push notification in a Cordova hybrid app, need guidance/help in identifying whether I am doing it the right way?
Here is the flow ->
User recieved push notification while app is in background -> Tap on the notification from the notification center -> App is opened (invoked) -> Get the custom payload values -> perform a JS Callback using stringByEvaluatingJavaScriptFromString -> The JS function will do the necessary actions the app has to do.
Here is my code where I am doing it
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[Pushbots sharedInstance] receivedPush:userInfo]; //Using Pushbots notification platform
NSString* Value1 = [userInfo objectForKey:#"val1"];
NSString* Value2 = [userInfo objectForKey:#"val2"];
NSString * jsCallBack = [NSString stringWithFormat:#"testfromdelegate('%#','%#')", Value1, Value2];
NSLog(#"%#", jsCallBack);
[self.viewController.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
}
Everything works till my NSLog(#"%#", jsCallBack);, and it fails at the jsCallBack. I understood that by the time my jsCallBack is being called, the webView is not yet ready, as the app is invoked from background.
What do I do to make this work? How to check whether my webView is ready, and then perform my jsCallBack?
How do I do this when the app is not running at all? For e.g., a user received a Whatsapp message and the app is not in background/foreground. But when the user taps on the message from the notification center, the whatsapp app opens with the user chat screen.
PS: All this code perfectly works when the app is in foreground. That means, the webView is already there and so jsCallBack has no problem.
If the app is in open or in background, then you can execute the javascript directly:
//This will be called if the app was open, in the foreground (active) or in the background and you reopen it from a push message
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[Pushbots sharedInstance] receivedPush:userInfo]; //Using Pushbots notification platform
NSString* Value1 = [userInfo objectForKey:#"val1"];
NSString* Value2 = [userInfo objectForKey:#"val2"];
NSString * jsCallBack = [NSString stringWithFormat:#"testfromdelegate('%#','%#')", Value1, Value2];
NSLog(#"%#", jsCallBack);
// If the app is in the foreground just execute the javascript
if ( application.applicationState == UIApplicationStateActive ) {
[self.viewController.webView stringByEvaluatingJavaScriptFromString: jsCallBack];
} else {
// If the app was in background, then force to execute the js code on the main thread
[self.viewController.webView performSelectorOnMainThread:#selector(stringByEvaluatingJavaScriptFromString:) withObject:jsCallBack waitUntilDone:NO]
}
}
If the app was completelly closed, then you can add an observer that will listen for the CDVPageDidLoadNotification (cordova finish loading), so you store the userInfo in a new variable myUserInfo and wait until the page is loaded to use it:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
//Don't remove the existing code, just paste this before the return YES
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(cordovaDidLoad) name:CDVPageDidLoadNotification object:self.viewController.webView];
//This will be called if the app was closed completelly and you open it from a push
if (launchOptions) { //launchOptions is not nil
NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
self.myUserInfo = userInfo;
}
return YES;
}
Then, when cordova is loaded this method will be called:
- (void)cordovaDidLoad {
//Check that myUserInfo isn't null, that will mean that the apps was completelly closed and it was opened from the push notification
if (self.myUserInfo) {
NSString* Value1 = [self.myUserInfo objectForKey:#"val1"];
NSString* Value2 = [self.myUserInfo objectForKey:#"val2"];
NSString * jsCallBack = [NSString stringWithFormat:#"testfromdelegate('%#','%#')", Value1, Value2];
NSLog(#"%#", jsCallBack);
[self.viewController.webView stringByEvaluatingJavaScriptFromString: jsCallBack];
}
}
I am using notification in one of my app ,when app is active , notification is received , i process data and everything is fine , but I do not receive any notification when app is in background or killed.What is the issue can any one plz help me ?
Thank you!
Here is what I doing so far
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
tokenstring = [[NSString alloc] initWithFormat:#"%#",deviceToken];
tokenstring = [tokenstring stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
tokenstring = [[NSString alloc]initWithFormat:#"%#",[tokenstring stringByReplacingOccurrencesOfString:#" " withString:#""]];
NSLog(#"TokeinID:>> %#",tokenstring);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"didReceiveRemoteNotification: %#",userInfo);
AudioServicesPlaySystemSound(1002);
//Some code or logic
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(#"didFailToRegisterForRemoteNotificationsWithError: %#",err.description);
}
When you receive remote notifications, -application:didReceiveRemoteNotification: is only called when your app is in the foreground. If your app is in the background or terminated, then the OS may display an alert or play a sound (depending on the aps dictionary in the notification), but the delegate method is not called.
The remote notification received in the background will only passed to your application if it is launched with that notification's action button, and then you need to look at the launch options dictionary on -application:didFinishLaunchingWithOptions: to see the content of the notification.
If you're looking at new content fetching/remote notification background support with iOS7, check Will iOS launch my app into the background if it was force-quit by the user? and see if that helps, as there are very specific circumstances that those functions work in.
little code ..
When app is not running
(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
is called ..
where u need to check for push notification
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification) {
NSLog(#"app recieved notification from remote%#",notification);
[self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
}else{
}
I am trying to test my app on iPod touch 4, however I need APNS support.
I found out that neither delegate callbacks for APNS is called.
I've searched through the Technical Note, and it doesn't help (for the same code works for my iPad2, that the device token is returned).
Any idea on this?
I am using iPod touch 4 with iOS 5.1.1
Edit:
Ah, more information provided here.
I have create a provisioning profile before as well as all those certificates. That's why I can work that out with iPad.
Then today, I just added my iPod touch 4 as the development device and downloaded the provisioning profile again. Will this act not registering my iPod touch 4 to be able for handling APNS job????
Edit 2:
As requested:
Actually I just do whatever Apple requested me to do,
that I included
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge)];
//Other codes...
}
Then, the two delegates callback methods
#pragma mark - For Apple Push Notification Service
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *str = [NSString
stringWithFormat:#"%#",deviceToken];
NSString *devToken = [[[[str description]
stringByReplacingOccurrencesOfString:#"<"withString:#""]
stringByReplacingOccurrencesOfString:#">" withString:#""]
stringByReplacingOccurrencesOfString: #" " withString: #""];
NSLog(#"%#", devToken);
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults)
{
[standardUserDefaults setObject:devToken forKey:#"device_token"];
[standardUserDefaults synchronize];
}
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(#"application:didFailToRegisterForRemoteNotificationsWithError: %#", error);
}
Actually, I just copy and paste what Apple provided me.
However the case is just as mentioned above, either method is called for iPad2 with iOS 5.0.1, iPhone with iOS 4.3 but not for my iPod Touch 4 with iOS5.1.1.
As the title says, when I first start up my app the dialog asking the user whether or not they want to receive push notifications doesn't appear. However if you go into settings the app appears in the push noticifications section, and the app DOES receive the push notification.
I'm using this to register for push, have I missed something out:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
I'm led to believe that if this is in didFinishLaunchingWithOptions that the prompt for the user would automatically appear?
Edit: I forgot to add that I do also have a didRegisterForRemoteNotificationsWithDeviceToken function in there too, to retain the token. As I say the notifications work in the sense that the user receives them, but the "do you want to receive them" popup never appears - which I would imagine are grounds for rejection by Apple.
Something like this is needed to retain the token:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *token = [[[deviceToken description]
stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]]
stringByReplacingOccurrencesOfString:#" "
withString:#""];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:[NSString stringWithFormat:#"%#", token] forKey:#"DeviceToken"];
NSLog(#"APNs Device Token: %#", token);
}
See this thread also: didRegisterForRemoteNotificationsWithDeviceToken - Push Notifications