iOS 推送,刪除指定推送消息或者撤回某條推送

iOS 推送,刪除指定推送消息

遠程推送經常會出現收到重複推送的問題,或者想刪除某條推送消息的問題,本文將詳細說明

靜默推送

  1. 在 iOS10 之後 Apple 新增了靜默推送的功能,使 App 可以在收到推送之後執行一段代碼,具體能執行多長時間沒有測試過
  2. 服務端往蘋果 APNS 發送如下消息內容可以激活靜默推送能力,使 App 具有收到推送執行相關功能

{aps:{"content-available":1}}

App 推送處理

  1. App 在收到推送之後使用新的推送框架發送本地推送
  2. 根據某個屬性,結合之前發送的本地推送的 identifier 可以做到消息攔截或者刪除某條消息的邏輯
  3. 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);
}

遠程推送測試工具

  1. PushMeBy
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章