AppDelegate is not listening to APNS Push Notification - iphone

When my app enters background, AppDelegate is not listening to the new push notification from APNS. But strange thing is that it doesn't happen all the time. Sometimes it works, sometimes it does not. I can't figure out why.
Here is my sample code for listening APNS Notification
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)];
}
if (launchOptions != nil)
{
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
}
}
return YES;
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
NSLog(#"Received notification: %#", userInfo);
[self application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:nil];
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"Received notification background: %#", userInfo);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC),
dispatch_get_main_queue(), ^{
// Check result of your operation and call completion block with the result
completionHandler(UIBackgroundFetchResultNewData);
});
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}

In background/closed mode,no code will execute and none of the delegate method will be invoked.This time push notification is handled by OS itself.The app badge is set by push notification payload value coming from server side.
If you are not getting app badge in background,it is a badge number issue from server side.Check whether the push notification payload contains application badge field and set to values greater than 0.
This link have same issue and resolved it.

Try to change your line..
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];
}
in iOS 8 you have to use "UIUserNotificationTypeBadge"...
Hope this will work for you..

Related

AppleWatch : On button tap from the the AppleWach Open URL on iPhone device

I want to open url from the AppleWatch app button tap to the mobile application.
Please any one have any idea regarding to these?
Added this in your AppDelegate.m
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}
This is what you have to apply on button tap in Watch App
NSURL *url = // your URL
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:url, #"URLKey", nil];
[WKInterfaceController openParentApplication:dic reply:^(NSDictionary *replyInfo, NSError *error)
{
NSLog(#"%# %#",replyInfo, error);
}];
And you can receive this request in iPhone in AppDelegate.m
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply
{
//receive userinfo dictionary here to perform specific request from watch
NSString *url = [userInfo objectForKey:#"URLKey"];
// perform operation on url
reply(#{#"Notification Alert":[NSString stringWithFormat:#"%f",dist]});
}

how to handle push payload when the app closed?

i am sending push payload to my users with the follow content :
{"aps": {"alert": "Go To Google", "sound": "Default","url":"http://www.google.com"}}
everything goes well when the pp is running but in the background.
if i am receiving the push and the app is closed i am open it and nothing happen.
i am trying to redirect to this url in the payload.
again when the app is running from the background it goes well.
this is the implementation so far AppDelegate.m:
-(void)Redirect:(NSString*)url{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
NSLog(#"%#",url);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
RedirectedUri = [[userInfo objectForKey:#"aps"] objectForKey:#"url"];
NSLog(#"%#",RedirectedUri);
[self Redirect:RedirectedUri];
}
need some help please.
Additionally, add the following to your code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self applicationDidFinishLaunching:application];
if (launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
RedirectedUri = [[dictionary objectForKey:#"aps"] objectForKey:#"url"];
[self Redirect:RedirectedUri];
}
}
return YES;
}

get device token inside -(BOOL)application:didFinishLaunchingWithOptions:

I don't know how to get device token (APN) inside
- (BOOL)application:didFinishLaunchingWithOptions:
the only place I can get the device token in AppDelegate is in
- (void)application:didRegisterForRemoteNotificationsWithDeviceToken:
where application:didRegisterForRemoteNotificationsWithDeviceToken will run asynchronous or after didFinishLaunchingWithOptions. So, is there any way to get device token in didFinishLaunchingWithOptions?? Because I want to pass it into my ViewController that pushed from didFinishLaunchingWithOptions.
Here is my sample code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
for (id key in launchOptions) {
NSLog(#"Key: %#, Value %#", key, [launchOptions objectForKey: key]);
}
AddViewController *avc = [[AddViewController alloc] init];
avc.managedObjectContext = self.managedObjectContext;
avc.pushToken = self.pushToken;
UINavigationController *uinav = [[UINavigationController alloc] init ];
[uinav pushViewController:avc animated:YES];
[_window addSubview:uinav.view];
[avc release];
[self.window makeKeyAndVisible];
// Let the device know we want to receive push notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
return YES;
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(#"My token is: %#", deviceToken);
self.pushToken = [NSString stringWithFormat:#"%#", deviceToken];
}
Thanks a lot for any help.
application:didRegisterForRemoteNotificationsWithDeviceToken: is your one and only chance to get the current device token. However, you can store the result in NSUserDefaults and read it from there on launch. Most of the time, if the app hasn't been uninstalled/reinstalled, the device token remains the same between launches.

How to manage notification when users click on badge

I have a question to ask about notifications. After some hours to learn how to implement push notifications on iPhone, it now arrives!
How do I manage users that click on badge or click view on alert? What happen when users click there?
I tried to send me some notification and the number on the icon of application in springboard increments. In which way clicking there it's possible to show a uiview to manage the notification arrived and show the message read and unread?
Is there a tutorial for it? I want to save all the messages inside a uitableview.
You want to read Handling Local and Remote Notifications
Basically in your application delegate, you want to implement:
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
and
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
And process the launchOptions / userInfo for the notification data.
How I normally process the data is:
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary* userInfo =
[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self processRemoteNotification:userInfo];
}
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[self processRemoteNotification:userInfo];
}
The format for userInfo is documented the The Notification Payload section.
e.g. the "aps" key will give you another NSDictionary, then looking up the "alert" key will give you the alert message that was displayed. Also, any custom data you send in the JSON payload will be in there as well.
NSDictionary *apsInfo = [userInfo objectForKey:#"aps"];
NSString *alertMsg = #"";
NSString *badge = #"";
NSString *sound = #"";
NSString *custom = #"";
if( [apsInfo objectForKey:#"alert"] != NULL)
{
alertMsg = [apsInfo objectForKey:#"alert"];
}
if( [apsInfo objectForKey:#"badge"] != NULL)
{
badge = [apsInfo objectForKey:#"badge"];
}
if( [apsInfo objectForKey:#"sound"] != NULL)
{
sound = [apsInfo objectForKey:#"sound"];
}
if( [userInfo objectForKey:#"Custom"] != NULL)
{
custom = [userInfo objectForKey:#"Custom"];
}

Method to handle errors sending push-notification

I m making an iOS notification service. But my device dont receive anything, i just take one tutorial from internet and make it.
I send more than 1 notification at time, maybe its this the problem? I have my app properly implemented (i can see it in settings).
The correct question for me is... Are there some methods you can use to see if there is any code to handle errors in sending the notifications?
Thank you.
In the Apple Documentation, look at :
Table 5-1 Codes in error-response
packet
You will find what you search.
Hi There are few methods in pushnotification whice are as below
First register your device by
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
Then use its delegate methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
{
NSLog(#"Before --- Token === %#",devToken);
NSString *strServerResponse = [[[devToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]]copy];
NSLog(#"%#",strServerResponse);
NSString * encodedURL = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)strServerResponse,NULL,(CFStringRef)#" ",kCFStringEncodingUTF8 );
NSString *DeviceToken=[Constant getUserDefaultValueForKey:#"DeviceToken"];
if([Constant checkStringNull:DeviceToken])
{
[Constant setUserDefaultValue:encodedURL forKey:#"DeviceToken"];
}
NSLog(#"After Encoding --- Token === %#",encodedURL);
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
NSLog(#"Error in registration. Error: %#", err);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"userInfo %#",userInfo);
[UIApplication sharedApplication].networkActivityIndicatorVisible=TRUE;
NSLog(#"\n\nData Received From Push Notification ===== %#",[userInfo description]);
[[UIApplication sharedApplication]setApplicationIconBadgeNumber:0 ];
}