iphone Launch Options

只愿长相守 提交于 2019-11-29 04:27:11

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:)

Rohain

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];
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!