iOS 推送,刪除指定推送消息
遠程推送經常會出現收到重複推送的問題,或者想刪除某條推送消息的問題,本文將詳細說明
靜默推送
- 在 iOS10 之後 Apple 新增了靜默推送的功能,使 App 可以在收到推送之後執行一段代碼,具體能執行多長時間沒有測試過
- 服務端往蘋果 APNS 發送如下消息內容可以激活靜默推送能力,使 App 具有收到推送執行相關功能
{aps:{"content-available":1}}
App 推送處理
- App 在收到推送之後使用新的推送框架發送本地推送
- 根據某個屬性,結合之前發送的本地推送的 identifier 可以做到消息攔截或者刪除某條消息的邏輯
- Demo
#import <UserNotifications/UserNotifications.h>
// userInfo 是遠程推送的字段, cancel 這個屬性表示是否刪除莫條推送消息
-(void)postLocalNotification:(NSDictionary *)userInfo{
UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc]init];
content.badge = @(123);
content.body = @"yangyudong,nihao,hahaha";
if ([userInfo[@"aps"][@"cancel"] integerValue]==1) {
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[@"Hello123"]];
completionHandler(UIBackgroundFetchResultNoData);
return;
}
content.userInfo = userInfo;
UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"Hello123" content:content trigger:nil];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];
completionHandler(UIBackgroundFetchResultNewData);
}
遠程推送測試工具
- PushMeBy