im not getting any launch options after a push notification; heres the code i have but non of the NSLogs seem to print in the debug area.
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSString *itemName = [localNotif.userInfo objectForKey:@"aps"];
NSLog(@"Custom: %@", itemName);
} else {
NSLog(@"//////////////////////////");
}
when i open the app (via pressing view on the push notification) it goes to the didReceiveRemoteNotification script, im not sure if thats meant to happen..
thanks for reading.
Your application receives push notifications through multiple paths, depending on the state of your app when it is received.
If your app is not launched (not even suspended in background), the launchOptions
will contain the notification payload (key UIApplicationLaunchOptionsRemoteNotificationKey
).
If it is already running or suspended in background, the app will receive the notifications via application:didReceiveRemoteNotification:
in your application delegate.
The process is the same for local notifications (UIApplicationLaunchOptionsLocalNotificationKey in application:didFinishLaunchingWithOptions: and application:didReceiveLocalNotification:
)
an error spotted:
NSDictionary *remoteNotif = [launchOptions objectForKey:
UIApplicationLaunchOptionsRemoteNotificationKey];
If you want to receive the remote notification, NSDictionary
should be used not UILocalNotification
The remote notification is a payload, containing arguments, not a local notification. You might want to look at this url question:
Crash when handling remote notification when app not running
If you want to do local notification, change it like Ludovic's suggestion
The answers given above are correct. I largely use the following snippet in my application:didFinishLaunchingWithOptions:
if let remoteNotifPayload = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
notificationsController.didReceiveRemoteNotification(with: remoteNotifPayload)
}
If you want a local notification (I assume with your var name) replace UIApplicationLaunchOptionsRemoteNotificationKey
by UIApplicationLaunchOptionsLocalNotificationKey
and this should work.
(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Alert message: %@",[[userInfo valueForKey:@"aps"] valueForKey:@"alert"]);
}
You can use NSUserDefaults
to do the trick. In your AppDeligate.m
, set a bool to YES
in the first time. So after that it never gets to NO
.
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//add this if loop
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}