iOS 通知

打印 上一主题 下一主题

主题 552|帖子 552|积分 1656

iOS 通知分为当地推送和远程推送两类
一. 当地推送使用流程

1. 注册通知

  1. //请求通知权限
  2. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  3. [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
  4.     if (error) { NSLog(@"request authorization error: %@", error); }
  5. }];
  6. //注册通知
  7. [[UIApplication sharedApplication] registerForRemoteNotifications];
复制代码
2.推送通知

  1. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  2. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; //标题
  3. content.sound = [UNNotificationSound defaultSound];
  4.         
  5. NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
  6. content.badge = @(badge);
  7. content.body = message;// 本地推送一定要有内容,即body不能为空。
  8. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000
  9.     if (@available(iOS 15.0, *))
  10.     {
  11.         content.interruptionLevel = UNNotificationInterruptionLevelTimeSensitive;//会使手机亮屏且会播放声音;可能会在免打扰模式(焦点模式)下展示
  12.              // @"{"aps":{"interruption-level":"time-sensitive"}}";
  13.              // @"{"aps":{"interruption-level":"active"}}";
  14.              content.body = message;// 本地推送一定要有内容,即body不能为空。
  15.     }
  16. #endif
  17. // repeats,是否重复,如果重复的话时间必须大于60s,要不会报错
  18. //UNCalendarNotificationTrigger 如果是时间触发
  19. //UNLocationNotificationTrigger 地点触发
  20. UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.01  repeats:NO];
  21. /* */
  22. //添加通知的标识符,可以用于移除,更新等搡作
  23. NSString * identifier = [[NSUUID UUID] UUIDString];
  24. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
  25. [center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error)
  26. {
  27.     if(!error)
  28.     {
  29.         dispatch_async(dispatch_get_main_queue(), ^{
  30.                      
  31.         });
  32.     }
  33. }];
  34. //点击本地通知回调
  35. /*API_DEPRECATED("Use UserNotifications Framework's -[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] or -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]"*/
  36. - (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
  37. {
  38.     //
  39. }
复制代码
二.远程推送(APNS)

1. 远程推送的五步骤

1).应该步伐注册APNS消息推送
2).ios 从APNS Server得到devicetoken.
3).应用程将devicetoken发送从自己的服务器。
4).服务器将消息发送给APNS服务器。
5).APNS发送给app
苹果官司方给机制解释图


2. 远程推送大小限定

远程通知负载的大小根据服务器使用的API不同而不同
当使用HTTP/2 provider API时,负载最大为4kB;当使用legacy binary interface时,负载最大为2kB。当负载大小凌驾规定的负载大小时,APNs会拒绝发送此通知 
3.推送通知实现

1)首先 设置 app 的bundle Id 支持 通知推送



2)申请推送通知证书,这个证书主要是服务器必要使用的

推送证书的申请过程和别的证书类似


3)app 注册通知 这部分和当地通知是一样的。

  1. //请求通知权限
  2. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  3. [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
  4.     if (error) { NSLog(@"request authorization error: %@", error); }
  5. }];
  6. //注册通知
  7. [[UIApplication sharedApplication] registerForRemoteNotifications];
复制代码
4)收到APNS 发送的token 并发送给服务器

  1. - (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  2. {
  3.     //注册推送消息成功
  4.     //收到deviceToken并发送给服务器
  5.     NSLog(@"deviceToken = %@", deviceToken);
  6. }
  7. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error API_AVAILABLE(ios(3.0))
  8. {
  9.     //注册推送消息失败
  10. }
复制代码
服务器收到token后,必要发送消息时,就可以把token,消息内容和证书一起发送给
APNS服务器,APNS就推送给app了。
5)app收到推送通知后的处置惩罚

1> app被杀死时,用户点击通知会在didFinishLaunchingWithOptions获取通知内容
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. ...
  3.    
  4. // 注册通知类型
  5. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  6. [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
  7.         if (error) { NSLog(@"request authorization error: %@", error); }
  8. }];
  9. [[UIApplication sharedApplication] registerForRemoteNotifications];
  10. // app 被杀死时用户点击通知 获取推送内容
  11. NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
  12. if (userInfo) {
  13.    //能过点击通知启动app
  14.    NSLog(@"userInfo = %@", userInfo);
  15. }
  16. ...
  17. }
复制代码
2> app在背景,点击通知
  1. //老版处理
  2. /*- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  3. {
  4.     DDLogDebug(@"userInfo ----->%@",userInfo);
  5. }*/
  6. //新版处理
  7. - (void) application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler
  8. {
  9.     DDLogDebug(@"userInfo ----->%@",userInfo);
  10. }
复制代码

三.推送功能的测试

有个推送测试的工具 SmartPush
下载地址:
1、Github地址:https://github.com/shaojiankui/SmartPush
 2、release安装包:https://github.com/shaojiankui/SmartPush/releases
 

3、消息体格式: 
  1. {
  2.   "aps": {
  3.     "alert": {
  4.       "title": "新消息",
  5.       "body": "您有一条新的消息!"
  6.     },
  7.     "badge": 1,
  8.     "sound": "default"
  9.   },
  10.   "customData": {
  11.     "type": "message",
  12.     "id": "12345"
  13.   }
  14. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

用户云卷云舒

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表