三、<iOS 远程推送> 静默推送

作者: Dosun | 来源:发表于2017-07-27 16:17 被阅读420次

静默推送是干嘛的?有什么作用,苹果为何提供静默推送?如何去触发静默通知?

一、静默通知调用方法的研读

通过观看 WWDC ,方知有个静默通知东东,于是想着如何实现它,它有什么注意点?通过后面查询资料才知道,静默远程通知会调用如下的方法。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler NS_AVAILABLE_IOS(7_0);

上面的方法作用是告诉应用,远程通知已经来了,远程通知捎点东西,让应用自己去拿一下。


参数说明

  • application application 单例对象。

  • userInfo 这个参数是远程通知带来的东西,userInfo 里面可能包含 badge 和 通知声音,和通知展示给用户的信息,通知的标识和自定义的数据哦。远程服务器推送的内容是 Json 格式的字典,而 iOS 会将其推送的内容转化成 NSDictonary 对象。

  • handler 当下载操作完成时,handler 将会被执行。当调用 handler 方法时,请传入 UIBackgroundFetchResult 枚举值最能描述下载操作结果。你一定要尽快的调用 handler 方法


UIBackgroundFetchResult 枚举的意思


二、讨论
  • 1、 静默通知能下载多久?
    有30秒的下载时间
    官方说明:Your app has up to 30 seconds of wall-clock time to process the notification and call the specified completion handler block. In practice, you should call the handler block as soon as you are done processing the notification。
  • 2、静默通知能在前台下载吗?
    已经验证,是前台和后台都能下载。

  • 3、静默通知后台下载有限制吗?
    有限制,苹果会监听下载的时间、电量消耗。如果应用处理静默通知下载去花费太多的电量,将来的静默通知可能不能及时唤醒应用的。
    官方说明: The system tracks the elapsed time, power usage, and data costs for your app’s background downloads. Apps that use significant amounts of power when processing remote notifications may not always be woken up early to process future notifications.


三、配置和代码实现
  • 1、配置
    如果你不明如何配置远程推送证书,请参考从零开始创建iOS远程推送证书里面有,教你如何配置推送证书。
    另外要在 Xcode 中操作两个步骤,一个是允许后台下载,另一个是允许接受远程通知。
    允许后台下载
允许接受远程通知
  • 2、代码
#import "AppDelegate.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

@interface AppDelegate ()<UNUserNotificationCenterDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //注册通知
    if ([[UIDevice currentDevice].systemVersion doubleValue]>= 10.0) {
        //iOS 10 特有
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        [center requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (granted) {
                NSLog(@"打印成功");
            }
        }];
        center.delegate = self;
        
    }else if([[UIDevice currentDevice].systemVersion doubleValue]>8){
        //分类
        UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
        
        //分类标识
        category.identifier = @"iOS 8 Category id";
        
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:[NSSet setWithObject:category]];
        
        [application registerUserNotificationSettings:settings];
    }
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    return YES;
}
#pragma mark - iOS 10 通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    NSLog(@"helloworld");
    completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}
// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from applicationDidFinishLaunching:.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
    
    completionHandler();
}

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    NSLog(@"%@",[NSString stringWithFormat:@"device Token %@",deviceToken]);
}


-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    if (error) {
        NSLog(@"%@",error);
    }
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    NSLog(@"iOS 10 以下收到通知");
}

#pragma mark -  静默通知
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    NSDictionary *dict = userInfo[@"aps"];
    NSLog(@"%@",dict[@"name"]);
    completionHandler(UIBackgroundFetchResultNewData);
}

@end

四、推送远程内容

提供一个强大的基于 MAC 的推送服务器,SmartPush。运行 Xcode 会出现如下图的弹框。

Snip20170727_42.png

注意:请使用最新的 notification 格式

  • 不能加alert,如果加入了alert就不是静默推送了,本人试过。
  • 务必加"content-available" : 1
  • sound感觉也不能加,加入的话就不是静默推送,但是我在测试中有加入,也是可以的,建议不要加。

服务器推送的内容如下。

{
    "aps": { "content-available" :  1,"name":"oliver"
            }
}

如果服务器推送的内容包含 alert 就不是静默推送了,就是 一般的远程推送啦。

 {
 "aps": {
 "alert": "This is some fancy message.",
 "badge": 1,
 "sound": "default",
 "mutable-content": "1",
 "imageAbsoluteString": "http://www.gaoxiaogif.com/d/file/201707/small1dd8ecd6f646f3785778c92ae68bcfda.gif"
 ,
 "title" :"noticefyTitle",
 "subtitle":"subtittle","fileType":"gif"
 }
 }

五、结语

静默通知是让应用在后台悄悄的下载东东,这样用户启动应用时,会给用户一种惊喜,苹果还是为了提高用户体验出发的。
如果不正确的地方,请告知,感恩!!

相关文章

  • 三、<iOS 远程推送> 静默推送

    静默推送是干嘛的?有什么作用,苹果为何提供静默推送?如何去触发静默通知? 一、静默通知调用方法的研读 通过观看 W...

  • iOS静默推送进阶知识

    iOS静默推送进阶知识 iOS静默推送进阶知识

  • iOS 远程推送通知

    iOS 远程推送通知 分分钟搞定IOS远程消息推送 iOS推送通知的实现步骤 推送通知iOS客户端编写实现及推送服...

  • 二、<iOS 推送> 本地推送

    最近,再次看路遥 《平凡的世界》时,多多少少有自己的影子在里面,觉得没有什么爱与不爱的,在一起就是一种幸福吧。就像...

  • 推送相关知识笔记

    本地推送 远程推送静默推送,实际上是普通推送的一种特殊状态。静默推送是不允许带 alert badge sound...

  • iOS 静默推送 <Remote Notification

    iOS静默推送 1、主要实现的功能:在用户察觉不到的情况下<没有声音,没有振动,没有弹框提示>进行远程推送,推送完...

  • swift 消息推送

    消息推送本身不难,基本分为远程推送(包含静默推送,特殊的远程推送)和本地推送;在实际项目中基本都会使用到,比较头痛...

  • iOS 关于远程推送的知识普及

    首先简单介绍一下iOS下推送通知吧!,推送通知分为有local本地推送,remote远程推送,而iOS7之后对远程...

  • iOS 远程消息推送 APNS推送原理和一步一步开发详解篇(新手

    iOS 远程消息推送 APNS推送原理和一步一步开发详解篇(新手推荐) iOS 远程消息推送 APNS推送原理和一...

  • 推送通知-远程推送

    iOS远程推送通知 远程推送服务,APNs(apple push notification servers) 所有...

网友评论

  • IT民工高先生:请问一下后台的网络请求如何实现的,我这边用AFN的网络请求并不能正常请求到数据。谢谢。
    IT民工高先生:@Dosun 我大概知道是,但是我用AFN正常下载,请求错误,和前台不一样,知道要怎么做吗?
    Dosun:@IT民工高先生 只能下载30秒的东东
  • 溪浣双鲤:稻香哥,发的真及时,给你赞个
    Dosun:对你有用就好,哈哈

本文标题:三、<iOS 远程推送> 静默推送

本文链接:https://www.haomeiwen.com/subject/odpzkxtx.html