首先,您必须在Xcode您的应用中enable push notification。您可以使用Apple指导文档,或者点击Xcode中您的工程,选择“Capabilities”,然后打开Push Notification设置。
要支持push notification,在下列委托方法中调用特定的Tapjoy方法:
// called when the remote notification is registered
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[Tapjoy setDeviceToken:deviceToken];
}
// called when the user get push message while playing the app
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
[Tapjoy setReceiveRemoteNotification:userInfo];
}
不要忘记注册push notification service。否则,您将不会看到允许访问的确认对话框。
// Registering for remote notifications
if (NSClassFromString(@"UNUserNotificationCenter")) {
// iOS 10+ Notifications
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[application registerForRemoteNotifications];
});
}
}];
} else if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
// iOS 8 - 9 Notifications
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
详细信息请参考iOS Local and Remote Notification Programming Guide。
发送push notifications需要有Apple push notification service证书。参考Local Notifications and Remote Notifications Guide以获得更多信息。当您取得了证书后,上传其到Tapjoy控制面板。更多信息请参考How to Configure Push Certificates 。请点击Tapjoy控制面板顶部的“设置” -> “应用设置”,然后选择左菜单栏的”Push Certificate”来上传您的证书。
当用Tapjoy发送推送通知时,您可以发送自定义负载(填入控制面板但用户不可见的字符串)。自定义负载可以为任意字符串,您的app可以用您设定的反应方式显示字符串。常见应用包括奖励用户反馈通知消息,在应用中通过自定义URL给用户发送特定位置。想要了解如何在您的app上自定义推送负载,请点击自定义负载文件。
为了使您的应用程序能够读取通过此方法发送的信息,您需要实现一些代码。 自定义有效负载设置为userInfo的p值。 并将适当的代码添加到didFinishLaunchingWithOptions
和didReceiveRemoteNotification
中,如下所示:
// called when the user clicks the push message
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
…
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo != nil) {
NSString* payload = [userInfo objectForKey:@"p"];
if (payload != nil) {
NSLog(@"Tapjoy push notification with payload: %@", payload);
}
}
...
}
// called when the user gets push message while playing the app
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
...
NSString *payload = [userInfo objectForKey:@"p"];
if (payload != nil) {
NSLog(@"Tapjoy push notification with payload: %@", payload);
}
...
}