美文网首页
IOS原生推送

IOS原生推送

作者: wenju | 来源:发表于2020-05-07 10:37 被阅读0次

1.注册
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
[center setDelegate:self];
UNAuthorizationOptions type = UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert;
[center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
// DBLog(@"注册成功");
}else{
// DBLog(@"注册失败");
}
}];
[application registerForRemoteNotifications];
}

2.通知方式

  • (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
    completionHandler(UNNotificationPresentationOptionBadge|
    UNNotificationPresentationOptionSound|
    UNNotificationPresentationOptionAlert);
    }

3.注册结果

  • (void)application:(UIApplication *)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    /// Required - 注册 DeviceToken
    // [JPUSHService registerDeviceToken:deviceToken];
    // SSLog(@"deviceToken == %@",deviceToken);
    NSString *string = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
    SSLog(@"wenju_deviceToken == %@ %@",deviceToken, string);
    NSMutableString *deviceTokenString = [NSMutableString string];
    const char *bytes = deviceToken.bytes;
    NSInteger count = deviceToken.length;
    for (int i = 0; i < count; i++) {
    [deviceTokenString appendFormat:@"%02x", bytes[i]&0x000000FF];
    }
    SSLog(@"wenju_deviceToken == %@", deviceTokenString);
    self.deviceToken = deviceTokenString;
    }

  • (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(nonnull NSError *)error{
    // 注册token失败
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
    }

相关文章

网友评论

      本文标题:IOS原生推送

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