关闭

Remote Notification (一)

287人阅读 评论(0) 收藏 举报
分类:
Configuring Remote Notification Support

远程通知甚至当你的app没有在设备上运行时也会将通知投递到用户设备端。大部分工作的实现依赖于你公司的server,但是你夜必须做如下的支持才可以:

开启你的app对远程通知的支持选项
在app加载期间注册APNs
实现对于远程通知的处理支持

Enabling the Push Notifications Capability

控制远程通知的app必须有合适的权利。你使用xcode中的capabilities pane来实现这些配置。

在工程导航中选择你的工程

在编辑器中,选择你的ios app target

选择capabilities tab

开启push notification capability image ../Art/push_notification_capability_2x.png

xcode将需要的权利添加到你的工程中

没有合适权限的app会在app store审核期间被拒。在测试过程中,没有合适权限的情况下注册apns会报错。
Registering to Receive Remote Notifications


每次你的app加载的时候,他必须注册了apns。你注册apns的方法根据不同的平台可能不同。主要的步骤如下:

使用平台特定的apis来从apns获得device token

将device token发送给你的server

对于特定的app和设备来说,device tokens是唯一的。你负责开启连接server的网络,并且将device token和其他的数据连同发送给server。在server端,使用device token和任何其他的数据来产生特定设备的远程通知。当把远程通知发送到一个设备上时,你必须总是包含设备token和数据当你将远程通知发送给一个设备的时候。

不要缓存device token,应当总是从系统来获得它们。尽管device token对于app和设备来说是唯一的,但是它们可以过段时间就发生变化。device token可以在任何时间改变,但是会保证当用户通过设备备份恢复设备的时候和之前的是不一样的。当用户把你的app安装到一个新的设备的时候,当用户重新安装操作系统的时候。都会改变device token。

IMPORTANT

当device token改变了,用户必须在远程通知被投递之前加载一次你的app。

watchos上运行的apps不会显示地注册远程通知。

Obtaining a Device Token on iOS and tvOS

在ios和tvos,通过registerForRemoteNotifications方法来获得device token。推荐你在app正常加载的时候来调用这个方法。当你第一次调用这个方法的时候,app对象从apns请求device token。当initial被调用后,app对象仅仅当device token改变的时候才会和apns通讯。否则,他会很快就返回这个device token。

app对象异步通知代理是否成功获取device token。你使用这些代理来处理device token或者处理错误。你必须实现以下的代理方法。

使用application:didRegisterForRemoteNotificationsWithDeviceToken:来获取device token并且发送到server端

使用application:didFailToRegisterForRemoteNotificationsWithError:来处理错误


NOTE

如果device token在app运行时改变了,app对象会调用合适的delegate方法来通知这个改变

Listing 4-1 shows how to fetch the device token for your iOS or tvOS app.

app调用registerForRemoteNotifications作为启动的设置部分。didRegisterForRemoteNotificationsWithDeviceToken方法将device token连接到sever端。如果在注册过程中发生了错误,app会立即禁用掉和远程通知相关的任何的特点。这个特性会在接收到device token后重新被可用。


Listing 4-1Registering for remote notifications on iOS


OBJECTIVE-C


- (void)applicationDidFinishLaunching:(UIApplication *)app {
    // Configure the user interactions first.
    [self configureUserInteractions];
 
   // Register for remote notifications.
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
 
// Handle remote notification registration.
- (void)application:(UIApplication *)app
        didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
    // Forward the token to your server.
    [self enableRemoteNotificationFeatures];
    [self forwardTokenToServer:devTokenBytes];
}
 
- (void)application:(UIApplication *)app
        didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    // The token is not currently available.
    NSLog(@"Remote notification support is unavailable due to error: %@", err);
    [self disableRemoteNotificationFeatures];
}


SWIFT


func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Configure the user interactions first.
    self.configureUserInteractions()
    
    // Register with APNs
    UIApplication.shared.registerForRemoteNotifications()
}
 
// Handle remote notification registration.
func application(_ application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){
    // Forward the token to your server.
    self.enableRemoteNotificationFeatures()
    self.forwardTokenToServer(token: deviceToken)
}
 
func application(_ application: UIApplication,
                 didFailToRegisterForRemoteNotificationsWithError error: Error) {
    // The token is not currently available.
    print("Remote notification support is unavailable due to error: \(error.localizedDescription)")
    self.disableRemoteNotificationFeatures()

}

NOTE

如果在你的app运行期间,device token改变了,app对象会再次调用合适的delegate方法来通知你的变化。


Listing 4-2 shows how to fetch the device token for your macOS app. The app delegate calls the registerForRemoteNotificationTypes: method as part of its regular launch-time setup, passing along the types of interactions that you intend to use. Upon receiving the device token, the application:didRegisterForRemoteNotificationsWithDeviceToken: method forwards it to the app’s associated server using a custom method. If an error occurs during registration, the app temporarily disables any features related to remote notifications. Those features are reenabled when a valid device token is received.

Listing 4-2Registering for remote notifications on macOS
OBJECTIVE-C

- (void)applicationDidFinishLaunching:(NSNotification *)notification {
    // Configure the user interactions first.
    [self configureUserInteractions];
 
    [NSApp registerForRemoteNotificationTypes:(NSRemoteNotificationTypeAlert | NSRemoteNotificationTypeSound)];
}
 
- (void)application:(NSApplication *)application
        didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // Forward the token to your server.
    [self forwardTokenToServer:deviceToken];
}
 
- (void)application:(NSApplication *)application
        didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Remote notification support is unavailable due to error: %@", error);
    [self disableRemoteNotificationFeatures];
}
SWIFT

func applicationDidFinishLaunching(_ aNotification: Notification) {
    // Configure the user interactions first.
    self.configureUserInteractions()
    
    NSApplication.shared().registerForRemoteNotifications(matching: [.alert, .sound])
}
 
// Handle remote notification registration.
func application(_ application: NSApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Forward the token to your server.
    self.forwardTokenToServer(token: deviceToken)
}
 
func application(_ application: NSApplication,
                 didFailToRegisterForRemoteNotificationsWithError error: Error) {
    // The token is not currently available.
    print("Remote notification support is unavailable due to error: \(error.localizedDescription)")
}
Handling Remote Notifications

The User Notifications framework offers a single code path for handling many tasks associated with local and remote notifications. Specifically, you handle the following tasks using the same techniques:
如果你的app在前台运行,那么你可以直接接收这个通知并且使它静音
如果你的app在后台或者没有运行:
当用户选择一个和通知相关联的自定义action时你可以给出respond
当用户dismisses这个通知或者加载你的app时你可以给出respond

除了通过user notifications framework来处理通知,你的app也通过app delegate来接收远程notifications。当一个远程notification到达时,系统会在app处于后台的时候处理用户的处理交互。


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:114422次
    • 积分:2579
    • 等级:
    • 排名:第14744名
    • 原创:80篇
    • 转载:245篇
    • 译文:33篇
    • 评论:2条
    最新评论