iOS 通知

iOS 通知分为本地推送和远程推送两类

一. 本地推送使用流程

1. 注册通知
objectivec 复制代码
//请求通知权限
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (error) { NSLog(@"request authorization error: %@", error); }
}];

//注册通知
[[UIApplication sharedApplication] registerForRemoteNotifications];
2.推送通知
objectivec 复制代码
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; //标题
content.sound = [UNNotificationSound defaultSound];
        
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
content.badge = @(badge);
content.body = message;// 本地推送一定要有内容,即body不能为空。

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000
    if (@available(iOS 15.0, *)) 
    {
        content.interruptionLevel = UNNotificationInterruptionLevelTimeSensitive;//会使手机亮屏且会播放声音;可能会在免打扰模式(焦点模式)下展示
             // @"{"aps":{"interruption-level":"time-sensitive"}}";
             // @"{"aps":{"interruption-level":"active"}}";
             content.body = message;// 本地推送一定要有内容,即body不能为空。
    }
#endif

// repeats,是否重复,如果重复的话时间必须大于60s,要不会报错
//UNCalendarNotificationTrigger 如果是时间触发
//UNLocationNotificationTrigger 地点触发
 UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.01  repeats:NO];

 /* */
//添加通知的标识符,可以用于移除,更新等搡作
NSString * identifier = [[NSUUID UUID] UUIDString];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) 
{
    if(!error)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
                     
        });
    }
}];


//点击本地通知回调 
/*API_DEPRECATED("Use UserNotifications Framework's -[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] or -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]"*/

- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    //
}

二.远程推送(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 注册通知 这部分和本地通知是一样的。
objectivec 复制代码
//请求通知权限
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (error) { NSLog(@"request authorization error: %@", error); }
}];

//注册通知
[[UIApplication sharedApplication] registerForRemoteNotifications];
4)收到APNS 发送的token 并发送给服务器
objectivec 复制代码
- (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    //注册推送消息成功
    //收到deviceToken并发送给服务器
    NSLog(@"deviceToken = %@", deviceToken);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error API_AVAILABLE(ios(3.0))
{
    //注册推送消息失败
}

服务器收到token后,需要发送消息时,就可以把token,消息内容和证书一起发送给

APNS服务器,APNS就推送给app了。

5)app收到推送通知后的处理

1> app被杀死时,用户点击通知会在didFinishLaunchingWithOptions获取通知内容

复制代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

...
    
// 注册通知类型
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (error) { NSLog(@"request authorization error: %@", error); }
}];
[[UIApplication sharedApplication] registerForRemoteNotifications];


// app 被杀死时用户点击通知 获取推送内容
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
   //能过点击通知启动app
   NSLog(@"userInfo = %@", userInfo);

}

...

}

2> app在后台,点击通知

复制代码
//老版处理
/*- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    DDLogDebug(@"userInfo ----->%@",userInfo);
}*/

//新版处理
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler
{
    DDLogDebug(@"userInfo ----->%@",userInfo);
}

三.推送功能的测试

有个推送测试的工具 SmartPush

下载地址:

1、Github地址:https://github.com/shaojiankui/SmartPush

2、release安装包:https://github.com/shaojiankui/SmartPush/releases

3、消息体格式:

复制代码
{
  "aps": {
    "alert": {
      "title": "新消息",
      "body": "您有一条新的消息!"
    },
    "badge": 1,
    "sound": "default"
  },
  "customData": {
    "type": "message",
    "id": "12345"
  }
}
相关推荐
2501_916008891 小时前
2026 iOS 证书管理,告别钥匙串依赖,构建可复制的签名环境
android·ios·小程序·https·uni-app·iphone·webview
YJlio6 小时前
《Windows 11 从入门到精通》读书笔记 1.4.9:全新的微软应用商店——“库 + 多设备同步”把它从鸡肋变成刚需入口
c语言·网络·python·数码相机·microsoft·ios·iphone
YJlio6 小时前
《Windows 11 从入门到精通》读书笔记 1.4.10:集成的微软 Teams——办公与社交的无缝衔接
c语言·网络·python·数码相机·ios·django·iphone
zhangjikuan896 小时前
SwiftUI 状态管理与架构实战
ios·架构·swiftui
KevinCyao8 小时前
iOS短信营销接口示例代码:Swift/Xcode集成营销短信API的完整开发教程
ios·swift
2501_915918418 小时前
iOS App 拿不到数据怎么办?数据解密导出到分析结构方法
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_916008898 小时前
iOS App 抓包看不到内容,从有请求没数据一步步排查
android·ios·小程序·https·uni-app·iphone·webview
RickeyBoy15 小时前
SwiftUI 如何实现 Infinite Scroll?
ios·面试
壹方秘境18 小时前
厌倦了那些看着像一个模版复刻出来的抓包工具,我开发了一款iOS端HTTPS抓包调试工具
ios·程序员·产品
开心就好20251 天前
不同阶段的 iOS 应用混淆工具怎么组合使用,源码混淆、IPA混淆
后端·ios