如何在应用程序处于后台时处理iOS远程通知
我正在通过apple push notification开发iOS推送通知功能,现在当我的应用程序处于后台或前台时,我会收到正确的通知,但我想在我的应用程序在后台时处理远程通知,基本上,当我的应用程序在后台时,它只是显示来自有效负载的警报消息。实际上,我只想自定义我的远程通知 代码:如何在应用程序处于后台时处理iOS远程通知,ios,objective-c,push-notification,Ios,Objective C,Push Notification,我正在通过apple push notification开发iOS推送通知功能,现在当我的应用程序处于后台或前台时,我会收到正确的通知,但我想在我的应用程序在后台时处理远程通知,基本上,当我的应用程序在后台时,它只是显示来自有效负载的警报消息。实际上,我只想自定义我的远程通知 代码: 在iOS 10中,首先必须在AppDelegate.h文件中设置unuservificationcenterdelegate @interface AppDelegate : UIResponder <UIA
在iOS 10中,首先必须在
AppDelegate.h
文件中设置unuservificationcenterdelegate
@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate,UNUserNotificationCenterDelegate>
现在为以下iOS10版本实现此方法
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
NSLog(@"Notification received: %@", userInfo);
if( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( @"10.0" ) )
{
NSLog( @"iOS version >= 10. Let NotificationCenter handle this one." );
return;
}
NSLog( @"HANDLE PUSH, didReceiveRemoteNotification: %@", userInfo );
else{
handler( UIBackgroundFetchResultNewData );
}
}
苹果在iOS10中引入了这两种方法来接收推送通知
也写下这些方法
// Receive displayed notifications for iOS 10 devices.
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSDictionary *userInfo = notification.request.content.userInfo;
NSLog(@"%@", userInfo);
completionHandler( UNNotificationPresentationOptionAlert );
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"Userinfo %@",response.notification.request.content.userInfo);
// completionHandler(UNNotificationPresentationOptionAlert);
}
就这样
请尝试此操作您在测试设备中使用的是哪个版本的iOS?对于iOS 10及以上版本,您必须执行一些其他操作iOS 10.0中没有一种方法是在应用程序回退时执行的
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
NSLog(@"Notification received: %@", userInfo);
if( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( @"10.0" ) )
{
NSLog( @"iOS version >= 10. Let NotificationCenter handle this one." );
return;
}
NSLog( @"HANDLE PUSH, didReceiveRemoteNotification: %@", userInfo );
else{
handler( UIBackgroundFetchResultNewData );
}
}
// Receive displayed notifications for iOS 10 devices.
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSDictionary *userInfo = notification.request.content.userInfo;
NSLog(@"%@", userInfo);
completionHandler( UNNotificationPresentationOptionAlert );
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"Userinfo %@",response.notification.request.content.userInfo);
// completionHandler(UNNotificationPresentationOptionAlert);
}