iOS基础知识整理iOS常用功能和UI效果的实现征服iOS

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

2017-07-27  本文已影响420人  Dosun

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

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

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

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

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


参数说明


UIBackgroundFetchResult 枚举的意思


二、讨论

三、配置和代码实现
允许接受远程通知
#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 格式

服务器推送的内容如下。

{
    "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"
 }
 }

五、结语

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

上一篇 下一篇

猜你喜欢

热点阅读