iOS 远程通知

iOS推送通知分为两种:
  • 远程推送通知(Remote Notification

苹果的远程通知服务 APNs(Apple Push Notification server) 允许设备和苹果的推送通知服务器保持链接,支持开发者推送消息到用户设备对应的应用程序.

  • 本地推送通知(Local Notification

本地通知服务需要使用 NSLocalNotification , 处理基于时间行为的通知.


解决 iOS App 通知中心 点击一条推送通知 删除一条 的问题

用一个空的本地推送解决角标为 0,通知栏消息全部消除的问题,APP进入前台以及进入后台都进行判断,代码实现如下:

+ (void)load {
    [[NSNotificationCenter.defaultCenter rac_addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil]
 subscribeNext:^(NSNotification *notification) {
         wb_execLocalNofitication(0);
     }];

    [[NSNotificationCenter.defaultCenter rac_addObserverForName:UIApplicationWillTerminateNotification object:nil]
 subscribeNext:^(NSNotification *notification) {
         /// 杀死应用发送延迟1秒,否则红点不消失
         wb_execLocalNofitication(1);
     }];
}

void wb_execLocalNofitication(NSInteger sinceTime) {
    if ([UIApplication sharedApplication].applicationIconBadgeNumber > 0) {
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        if (notification) {
            NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:sinceTime];
            notification.fireDate = fireDate;
            notification.timeZone = [NSTimeZone defaultTimeZone];
            notification.repeatInterval = 0;
            notification.alertBody = nil;
            notification.applicationIconBadgeNumber = -99;
            notification.soundName = nil;
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        }
    }
}

终端生成推送 *.pem 文件命令:
openssl pkcs12 -in ck.p12 -out ck.pem -nodes

推送相关代码及自测 push 功能请参照 Github https://github.com/huipengo/WBNotification

可能遇到的问题:
  • 程序不执行 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 方法,或者程序收不到推送通知,记得检查是否已打开下图的 push Notification 开关;
iOS 远程通知_第1张图片
push-Notifications.png

参考文献:
1、iOS 7 Background Remote Notification
2、How to remove a notification programmatically from iOS Notification Center.
3、https://www.cnblogs.com/qiqibo/archive/2012/08/25/2656856.html

你可能感兴趣的:(iOS 远程通知)