IOS Push Notification Issue

I am doing a project in which the push notification function is one of the key functions.
It works fine when I am in the application, I receive a notification and process this notification.

But the problem is that when I am in the background and receiving a notification, I see an icon on my application icon and when I click on the icon that launches my application, but the didReceiveRemoteNotification method didReceiveRemoteNotification not called, so I can not process this notification.

And another problem is that several times it shows a notification in the device notification list , but several times it is not.

When I enter my application by clicking on the notification list item, didReceiveRemoteNotification calls, and I can successfully process the notification. I am writing the following code in didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method

 NSDictionary* remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (remoteNotif != nil) { NSLog(@"didFinishLaunchingWithOptions\nNotification recieved:\n%@",remoteNotif); notificationData=[[NSDictionary alloc]initWithDictionary:remoteNotif]; [notif saveNotification:remoteNotif]; } 

Help me solve this problem. Thanks in advance.

+6
source share
3 answers

Things you do in the dofinishlaunch method are done in didReceiveRemoteNotification . When you come from the background, didfinishlaunch will not be called.

 - (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo { UIApplicationState state = [application applicationState]; if (state == UIApplicationStateActive) { //What you want to do when your app was active and it got push notification } else if (state == UIApplicationStateInactive) { //What you want to do when your app was in background and it got push notification } } 
+2
source

Apple's Guide to Programming Local and Push Notifications

Handling local and remote notifications

Let's look at possible scenarios when the system delivers local notifications or remote notifications to the application.

  • A notification is delivered when the application is not running in the foreground.

    In this case, the system presents a notification displaying a warning, icon icons, possibly sound reproduction.

    As a result of the notification presented, the user deletes the alert button action or click (or click) the application icon.

    If the action button is pressed (on an iOS device), the system launches the application, and the application calls its delegates application:didFinishLaunchingWithOptions: (if implemented); it is transmitted in the notification payload (for remote notifications) or the local notification object (for local notifications).

    If the application icon is displayed on an iOS device, the application calls the same method but does not provide any notification information . If the computer’s operating system X is on the application’s icon, the application calls applicationDidFinishLaunching: delegates, in which the delegate can receive the remote notification payload.

  • A notification is delivered when the application is running in the foreground.

    An application calls its delegates application:didReceiveRemoteNotification: (for remote notifications) or application:didReceiveLocalNotification: (for local notifications) and passes notifications or a local notification object to the payload.

So, in your case, when the application is running in the background, and when you press the notification / warning, the operating system brings your application to the forefront. Thus, it falls under the second paragraph.

You can implement the application:didReceiveRemoteNotification: method to get the notification payload if the action button is clicked. But when the application icon is clicked instead of the action message, the notification payload is not forwarded using the method. The only option is to contact your server and synchronize the data. In the end, according to Apple's policy, a push notification only tells you that there is data on the server, and in any case, you need to connect to the server and receive the data.

+2
source

I did the same in one of my applications. Cannot process notification when you click on the application icon. So what you can do is make a server call to get lastPushNotificationIdSync. You have to store your data somewhere on the server, so you need to check on your server what is the last lastPushNotificationId.

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions notificationContentDict = launchOptions; if([[launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"]count]>0){ application.applicationIconBadgeNumber = 0; NSString *key = [[launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] valueForKey:@"id"]; contentId = key; ////// you can use this content id ///// write your code here as per the requirement ..//// //// display your content on UI /// either get from server or local .../// [self displayContent:[launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"] application:application]; } else [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound]; return YES; } - (void)applicationDidBecomeActive:(UIApplication *)application { NSLog(@"badge count ..%d", application.applicationIconBadgeNumber); if( application.applicationIconBadgeNumber >0){ if(!notificationContentDict){ make a server call to get "latestPushNotificationIdSync" application.applicationIconBadgeNumber = 0; NSLog(@"badge count applicationDidBecomeActive.%d", application.applicationIconBadgeNumber); } } } 
0
source

Source: https://habr.com/ru/post/951104/


All Articles