ios10前台收到推送,当App在iOS 10的后台时,推送通知不会被收到

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.

My code is:

- (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 {

}

When App is in background state:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center

willPresentNotification:(UNNotification *)notification

withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

-- is not called at all.

I enabled push notifications and also remote notifications in background modes in App Capabilities. But App is still not receiving the notification.

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?

解决方案

For iOS 10, we need to call the 2 methods below.

For FOREGROUND state

- (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);

}

For BACKGROUND state

- (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();

}

Before that, we must add the UserNotifications framework and import in the AppDelegate.h file

#import

@interface AppDelegate : UIResponder

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值