我在我的应用程序中实现了远程通知!如果我的应用程序在后台并且推送消息已发送到我的设备,我会对此方法做出反应:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
...Do Stuff
}
当App处于Foreground或Background State时,这非常有用!但是,如果我的应用程序根本没有运行怎么办?!当应用程序没有运行时,我不能对Push Messages做出反应吗?我的意思是WhatsApp可以做到这一点,对吧?!
答案 0 :(得分:1)
如果用户点击通知中心的推送通知,您将在launchOptions中获得包含推送通知内容的信息,您可以使用以下代码检查应用程序是否已启动,点击推送通知,或者它也在那里,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
NSLog(@"LaunchOptions->%@",launchOptions);
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self performNotificationAction:userInfo];
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
// NSLog(@"userInfo->%@",userInfo);
[self performNotificationAction:userInfo];
}
-(void)performNotificationAction:(NSDictionary*)userInfo{
//Do the stuf whatever you want.
//i.e. fetch the message or whatever extra information sent in push notification
}
希望这有帮助。