Download Push Push data when you click on the application

I have an iPhone app implemented using push notification. I have successfully implemented push notification mainly. When I tap a push notification, the message will appear on the message label on the ViewController. However, when I open the icon (application), it does not return any notifications. I need to display a push notification not only when a push notification is clicked, but it should work if the iOS user just opens the application.

here is my code.

AppDelegate.m

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; } - (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo{ NSString *messageAlert = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"]; NSLog(@"Received Push Message: %@", messageAlert ); [[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:messageAlert]; 

on my ViewController.m

 - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserverForName:@"MyNotification" object:nil queue:nil usingBlock:^(NSNotification *note) { NSString *_string = note.object; messages.text = _string; //message }]; } } 

It will display a notification, although with the notification I clicked. But when I open the application, the notification should also display a message. How to do it? please help me.

+4
source share
3 answers

The device consists mainly of four push notification states. Consider one after another each case: -

CASE 1: when the application is really not loaded into memory (for example, when you launch it, a splash screen will appear, etc.), then the application is called: didFinishLaunchingWithOptions, and you can get push push as follows:

  NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if(remoteNotif) { //Handle remote notification } 

CASE 2: if the application is loaded into memory and is ACTIVE (for example, the application is open on the device), then only the application: (UIApplication *) didReceiveRemoteNotification: (NSDictionary *) userInfo is called. Normal, as you said, in this case.

CASE 3: if the application is loaded into memory, but is not ACTIVE and DOES NOT ASSUME (for example, you started the application, then pressed the home button and waited 10 seconds), and then you press the action button on the push notification, only doReceiveRemoteNotification is called . follow the approach below.

 -(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo { if([app applicationState] == UIApplicationStateInactive) { //If the application state was inactive, this means the user pressed an action button // from a notification. //Handle notification } } 

CASE 4: when the application is in the background, and then click the icon, and then according to Apple's documentation, you will not be able to receive all pending notifications.

Note To solve this problem, Apple introduces the concept of launching a background launch of an application in iOS 7. But until now, you cannot do this before developing iOS 6, which you must get stuck on it.

Hope this helps you!

+9
source

When the user clicks the application icon, the didReceiveRemoteNotification method does not work. You need to catch a notification in the app: didfinishLauncingWithOptions. Something like that:

 NSDictionary *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; NSLog(@"launchOptions: %@", launchOptions); NSLog(@"localNotif: %@", localNotif); if (localNotif) { NSDictionary *itemName = [localNotif objectForKey:@"aps"]; NSLog(@"dict: %@, aps: %@", localNotif, itemName); //your methods to process notification } 
+4
source

You can not. This is what the apple document says:

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 application icon is clicked on a computer running OS X, the application calls the delegate applicationDidFinishLaunching: a method in which the delegate can receive the remote notification payload.

You probably want to look at this question and answer .

+1
source

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


All Articles