注册远程通知ios10并捕获APN令牌 [英] Register remote notifications ios10 and catch the apn token

查看:43
本文介绍了注册远程通知ios10并捕获APN令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释如何使用iOS10提供的新UserNotification框架注册远程推送通知并捕获令牌吗?

Could anybody explain to me how to register for remote push notification and catch the token, using the new UserNotification framework provided by iOS10 ?

推荐答案

您可以像在iOS 8中一样注册到通知(这是为数不多的未更改通知的API之一).

You can register to notifications like you're already doing from iOS 8 (it's one of the few API for notifications that hasn't changed).

首先,在AppDelegate的 application:didFinishLaunchingWithOptions:方法中,为您的应用请求授权:

First, in the application:didFinishLaunchingWithOptions: method of the AppDelegate, request the authorization for your app:

UNUserNotificationCenter.current().requestAuthorization([.alert, .sound, .badge]) { (granted, error) in
    //here you can check the correct authorization    
}

这将显示通常的应用程序希望向您发送通知"警报.新的 requestAuthorization 方法的主要改进是,您可以管理直接在闭包中点击允许/不允许"按钮的行为.

This will show the usual "Application would like to send you notifications" alert. The main improvement of the new requestAuthorization method is that you can manage the behaviour of tapping on Allow / Don't Allow buttons directly in the closures.

接下来,使用iOS 8中提供的 UIApplication registerForRemoteNotifications 方法注册远程通知:

Next, register for remote notifications with the registerForRemoteNotifications method of UIApplication available from iOS 8:

UIApplication.shared().registerForRemoteNotifications()

...并最终通过您的通知服务器(例如Amazon,OneSignal等)来管理注册

...and finally manage the registration with your notifications server (like Amazon, OneSignal, etc...)

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    //if you need the token as a string, do this:
    let tokenString = String(data: deviceToken, encoding: .utf8)

    //call the notifications server for sending the device token
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print("Application failed to register for remote notifications")
}

参考

这篇关于注册远程通知ios10并捕获APN令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆