奇怪的注意事項(xiàng)(測試版本iOS15)
- 如果不設(shè)置普通推送delegate,則普通推送會(huì)走靜默推送回調(diào)。
- 程序每次卸載+重裝都會(huì)更新token。
- 即使彈窗用戶點(diǎn)擊不允許,依舊可以獲取token。
- 設(shè)置中的關(guān)閉通知,僅對普通推送有效,對靜默推送不起作用。同理,關(guān)閉后臺(tái)刷新只對靜默推送有效,對普通推送不起作用。
- 靜默推送會(huì)強(qiáng)行啟動(dòng)App,不管App是Active、Suspend、Killed或者任何情況,記住是任何情況。
- 靜默推送啟動(dòng)App會(huì)走
application:didFinishLaunchingWithOptions:,但是不會(huì)觸發(fā)UIApplicationDidBecomeActiveNotification。
- 靜默推送會(huì)自動(dòng)啟動(dòng)App,并且最長維持30秒,用戶再次點(diǎn)擊時(shí)不會(huì)再觸發(fā)
application:didFinishLaunchingWithOptions:。
- 蘋果后臺(tái)申請推送證書以及
Identifiers時(shí),只需要勾選Push Notifications即可實(shí)現(xiàn)兩種推送功能。
- 普通推送只有在點(diǎn)擊通知欄進(jìn)入App后,才會(huì)走對應(yīng)的回調(diào)。
- 網(wǎng)上的其他教程大部分在瞎扯淡。
推送代碼
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"啟動(dòng)了");
[self setNormalPushNotification];
return YES;
}
// 初始化普通推送
- (void)setNormalPushNotification {
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
NSLog(@"是否允許通知: %d", granted);
}];
[[UIApplication sharedApplication] registerForRemoteNotifications];
// ??注意: 如果不設(shè)置delegate,普通推送也會(huì)走didReceiveRemoteNotification
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
}
// App在前臺(tái)時(shí)收到普通推送
- (void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler {
NSDictionary* userInfo = notification.request.content.userInfo;
NSLog(@"前臺(tái)收到普通推送: %@", userInfo);
}
// App在后臺(tái)時(shí)點(diǎn)擊普通推送欄
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
NSDictionary* userInfo = response.notification.request.content.userInfo;
NSLog(@"后臺(tái)收到普通推送: %@", userInfo);
completionHandler();
}
// APNS注冊成功,返回token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
if (![deviceToken isKindOfClass:[NSData class]]) return;
const unsigned *tokenBytes = [deviceToken bytes];
NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
NSLog(@"deviceToken:%@",hexToken);
}
// APNS注冊失敗
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Error: %@", error);
}
// App在任何狀態(tài)下收到靜默推送
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"收到靜默推送: %@", userInfo);
[[NSNotificationCenter defaultCenter] postNotificationName:@"Recieve" object:nil];
UIApplicationState state = [UIApplication sharedApplication].applicationState;
if(state == UIApplicationStateBackground) {
NSLog(@"App在后臺(tái),運(yùn)行5秒");
} else if (state == UIApplicationStateActive) {
NSLog(@"App在前臺(tái)");
}
// 5秒后通知系統(tǒng)將處于后臺(tái)的App掛起(suspend)
[NSTimer scheduledTimerWithTimeInterval:5 repeats:NO block:^(NSTimer * _Nonnull timer) {
completionHandler(UIBackgroundFetchResultNewData);
}];
}
@end
推送JSON測試用例
// 靜默推送
{"aps":{"content-available":1}}
// 普通推送
{"aps":{"alert":"普通推送","sound":"default","userinfo":{"username":"tom"}}}
好用的推送測試軟件
https://github.com/onmyway133/PushNotifications/releases
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。