iOS SDK_推送功能

可开启或关闭全部推送、夜间推送、广告推送功能,并可使用本地推送功能。 使用推送功能时,需将广告推送设置为YES(广告推送值为NO时,无论一般/夜间推送如何设置,都不会收到推送。)

推送开启及关闭


如要使用推送功能,请使用下列代码。

#import <GamePot/GamePot.h>

// 开启/关闭推送接收
[[GamePot getInstance] setPushEnable:YES success:^{

} fail:^(NSError *error) {

}];

// 开启/关闭夜间推送接收
[[GamePot getInstance] setNightPushEnable:YES success:^{

} fail:^(NSError *error) {

}];

// 同时设置推送/夜间推送
// 如果是登录前需要获取推送/夜间推送权限的游戏,登录后必须调用以下代码。
[[GamePot getInstance] setPushStatus:YES night:YES ad:YES success:^{
    <#code#>
} fail:^(NSError *error) {
    <#code#>
}];

图片推送功能


为在iOS应用中接收并处理通知图片,按以下方法添加通知服务扩展程序。

  1. 在Xcode点击Target菜单选择Notification Service Extension后点击Next

  2. 输入Project Name后点击Finish

  3. 对已创建的Notification Service Extension模块的NotificationService.h文件,进行如下修改。

    // 导入GamePot/GamePotNotificationServiceExtension.h
    // #import <UserNotifications/UserNotifications.h>
    #import <GamePot/GamePotNotificationServiceExtension.h>
    
    // 取代UNNotificationServiceExtension继承GamePotNotificationServiceExtension
    // @interface NotificationService : UNNotificationServiceExtension
    @interface NotificationService : GamePotNotificationServiceExtension
    @end
  4. 对已创建的Notification Service Extension模块的NotificationService.m文件,进行如下修改。

    ...
    - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
        // self.contentHandler = contentHandler;
        // self.bestAttemptContent = [request.content mutableCopy];
    
        // Modify the notification content here...
        // self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
    
        // self.contentHandler(self.bestAttemptContent);
        [super didReceiveNotificationRequest:request withContentHandler:contentHandler];
    }
    ...
  5. 在已创建的Notification Service Extension模块依次点击Targets > Build Phases > Link Binary With Libraries菜单后添加GamePot.framework。

本地推送功能


可以不通过推送消息服务器,直接在设备自主显示推送。

如要通过注册推送在规定时间显示本地推送时,请使用下列代码。

...
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    // self.contentHandler = contentHandler;
    // self.bestAttemptContent = [request.content mutableCopy];

    // Modify the notification content here...
    // self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];

    // self.contentHandler(self.bestAttemptContent);
    [super didReceiveNotificationRequest:request withContentHandler:contentHandler];
}
...

取消已注册的本地推送

如要使用注册本地推送时获得的pushid值取消已注册的推送,请使用下列代码。

[[GamePot getInstance] cancelLocalPush:(int)pushId];

Last updated