When my App is not running and receives a Push Notification, if I click on that notification, the App is launched - but then it doesn\'t prompt the
When your app is not running or killed and you tap on push notification this function will trigger;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
so you should handle it like this,
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSString *json = [localNotif valueForKey:@"data"];
// Parse your string to dictionary
}
Try going to the didReceiveRemoteNotification
method and handling it there. In there you can check for the application states by doing conditionals for the applicationState
, for example checking if it's UIApplicationStateActive
or others.
I had the same problem but I finally could handle Push Notification when the is Inactive (not running) using Notification Service Extension. This solution was recommended by Apple Team Support and it is only for iOS 10, I implemented it and it works well! I leave the link:
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ModifyingNotifications.html
This is the procedure:
NOTE: Consider that you will use three identifiers: 1) Identifier for your app (you already have) 2) Identifier for your extension (you will create) 3) Identifier for App Group. (you will create)
Is necessary to enable App Groups to create a resource where you can save and read info for your app and the extension. Click on "Show Project Navigator". Into the list of targets select your main project. Then, click on "Capabilities" and switch on the option named "App Groups". Please add the identifier for App Group to identify share resources. You should do the same step for the target of extension that you created (Select target of extension - Capabilities - Switch on at "App Groups" - Add the same identifier for App Group)
You should to add the Identifier for your extension into Apple Developer Site for identify the Notification Service Extension, also you should to make new Provisional Profiles (Development, AdHoc and/or Production) and associate it with the new Provisional Profiles.
On both identifiers (App and Extension) you should edit them and enable "App Groups" Service in both of them. You should to add the Identifier for App group into the App Groups Services.
NOTE: Identifier for App and Identifier for Extension SHOULD HAVE THE SAME IDENTIFIER FOR APP GROUP.
Download the new Provisional Profile on Xcode and associate them with your Notification Service Extension. Please ensure you everything is ok with them.
After that, into "Capabilities" of your app and extension, open "App Groups" section and update them. The three steps - 1)Add the App Groups entitlements to your entitlements file, 2) App the App Groups feature to your App ID and 3) Add App Groups to your App ID - should be checked.
Come back to Project navigator and select the folder of you extension. Open the .m file. You will see a method called didReceiveNotificationRequest:(UNNotificationRequest *)request. Into this method you will create a different NSUserDefaults with SuiteName exactly equal to the Identifier for app group like this:
NSUserDefaults *defaultsGroup = [[NSUserDefaults alloc] initWithSuiteName: @"identifier for app group"];
Into this same method, get the body of the notification and save it into a NSMutableArray, then save it in the share resources. Like this:
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler{ self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; NSMutableArray *notifications = [NSMutableArray new]; NSUserDefaults *defaultsGroup = [[NSUserDefaults alloc] initWithSuiteName: @"Identifier for app group"]; notifications = [[defaultsGroup objectForKey:@"notifications"] mutableCopy]; if (notifications != nil){ [notifications addObject:self.bestAttemptContent.userInfo]; }else{ notifications = [NSMutableArray new]; [notifications addObject:self.bestAttemptContent.userInfo]; } [defaultsGroup setObject:notifications forKey:@"notifications"]; }
NSUserDefaults *defaultsGroup = [[NSUserDefaults alloc] initWithSuiteName: @"Identifier for app group"]; NSMutableArray *notifications = [[defaultsGroup objectForKey:@"notifications"] mutableCopy];
I hope to be clear with each step. I going to write a post to implement this solution with images in my page. Another point that you should consider is it doesn't work with Silent Push Notification.
try this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//register notifications
if([application respondsToSelector:@selector(registerUserNotificationSettings:)]) // ios 8
{
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
[application registerForRemoteNotifications];
}
else // ios 7
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge];
}
// open app from a notification
NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification)
{
//your alert should be here
}
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
return YES;
}
I tried @dianz's answer, It's not worked for me but I got a help from the answer.
In my case;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *apnsBody = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (apnsBody) {
// Do your code with apnsBody
}
Better be if you override didReceiveRemoteNotification
Here you go
NSDictionary * pushNotificationPayload = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(pushNotificationPayload) {
[self application:application didReceiveRemoteNotification:pushNotificationPayload];
}
This will again fire the didReceiveRemoteNotification
method and your push notification
code will execute as same as it runs during application run time.