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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值