我正在使用 FCM(Firebase 云消息传递)在 iOS 中发送推送通知.
I'm using FCM(Firebase Cloud Messaging) for sending push notifications in iOS.
当应用程序处于前台状态时,我能够收到通知.但是当应用程序处于后台状态时,没有收到通知.每当应用程序进入前台状态时,才会收到通知.
I'm able to receive the notification when App is in foreground state. But when the App is in background state, the notification is not received. Whenever the application will come to foreground state, only then will the notification be received.
我的代码是:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
// Print message ID.
NSDictionary *userInfo = notification.request.content.userInfo;
NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);
// Pring full message.
NSLog(@"%@", userInfo);
if( [UIApplication sharedApplication].applicationState == UIApplicationStateInactive )
{
NSLog( @"INACTIVE" );
completionHandler(UNNotificationPresentationOptionAlert);
}
else if( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground )
{
NSLog( @"BACKGROUND" );
completionHandler( UNNotificationPresentationOptionAlert );
}
else
{
NSLog( @"FOREGROUND" );
completionHandler( UNNotificationPresentationOptionAlert );
}}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
App处于后台状态时:
When App is in background state:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
-- 根本没有被调用.
-- is not called at all.
我在 App Capabilities 的后台模式中启用了推送通知和远程通知.但是App仍然没有收到通知.
I enabled push notifications and also remote notifications in background modes in App Capabilities. But App is still not receiving the notification.
我提到了一些 StackOverflow 问题,但无法解决问题.iOS 版本 10 中是否有任何要添加的内容或我的代码中有任何错误?
I referred to some StackOverflow questions but wasn't able to solve the issue. Is there anything to add in iOS version 10 or any mistake in my code?
对于iOS 10,我们需要调用下面2个方法.
For iOS 10, we need to call the 2 methods below.
对于 FOREGROUND 状态
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
NSLog( @"Handle push from foreground" );
// custom code to handle push while app is in the foreground
NSLog(@"%@", notification.request.content.userInfo);
completionHandler(UNNotificationPresentationOptionAlert);
}
对于背景状态
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
NSLog( @"Handle push from background or closed" );
// if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background
NSLog(@"%@", response.notification.request.content.userInfo);
completionHandler();
}
在此之前,我们必须添加UserNotifications
框架并导入到AppDelegate.h
文件中
Before that, we must add the UserNotifications
framework and import in the AppDelegate.h
file
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
这篇关于应用程序在 iOS 10 中处于后台时未收到推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!