我使用firebase在iOS中实现推送通知,目标是c。我有一个方法application:didReceiveRemoteNotification:fetchCompletionHandler
,它应该在应用程序在后台并且用户点击通知时触发,也应该在应用程序处于前台时触发,根据其描述。问题是,它只能在后台运行(或者当应用程序不运行时)。我是不是忘了什么?
谢谢你的帮助。
发布于 2017-06-30 12:05:39
对于iOS 10及更高版本,当应用程序在前台时,将调用以下方法:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
所以当app在前台时,您需要遵循这个方法来处理通知
发布于 2017-06-30 12:23:12
你可以使用下面的代码:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
//app is in foreground
//the push is in your control
UILocalNotification *localNotification =
[[UILocalNotification alloc] init];
localNotification.userInfo = userInfo;
localNotification.soundName =
UILocalNotificationDefaultSoundName;
localNotification.alertBody = message;
localNotification.fireDate = [NSDate date];
[[UIApplication sharedApplication]
scheduleLocalNotification:localNotification];
}
else
{
//app is in background:
//iOS is responsible for displaying push alerts, banner etc..
}
}
发布于 2017-06-30 12:07:14
iOS Firebase通知的标准示例是在AppDelegate中实现的,如completionHandler(UIBackgroundFetchResultNewData);
如果你喜欢在前台,你必须实现它。
https://stackoverflow.com/questions/44845998
复制相似问题