Unity SDK_推送功能

可允许或禁用一般推送、夜间推送、广告推送功能,可使用本地推送功能。 若使用推送功能,请将广告推送设置为true(广告推送值为false时,无论是否设置一般/夜间推送,皆不会推送通知。)

一般推送设置


如要设置一般推送,请使用下列代码。

  • 方式1

        GamePot.setPushStatus(bool pushEnable);
    
        /// 对推送状态变更的服务器通信成功
        public void onPushSuccess() {
        }
    
        /// 对推送状态变更的服务器通信失败
        public void onPushFailure(NError error) {
    
            // 推送状态变更失败时,请使用error.message显示错误消息。
        }

  • 方式2

    void GamePot.setPushStatus(bool pushEnable, GamePotCallbackDelegate.CB_Common);
    
        GamePot.setPushStatus(pushEnable, (success, error) => {
            if(success)
            {
                // 对推送状态变更的服务器通信成功
            }
           else
           {
                // 推送状态变更失败。请使用error.message显示错误消息。
            }
        });
        

夜间推送设置


如要设置夜间推送,请使用下列代码。

  • 方式1

    GamePot.setPushNightStatus(bool nightPushEnable);
    
        /// 对夜间推送状态变更的服务器通信成功
        public void onPushNightSuccess() {
        }
    
        /// 对夜间推送状态变更的服务器通信失败
        public void onPushNightFailure(NError error) {
    
            // 夜间推送状态变更失败时,请使用error.message显示错误消息。
        }

  • 方式2

        void GamePot.setPushNightStatus(bool nightPushEnable, GamePotCallbackDelegate.CB_Common);
    
        GamePot.setPushNightStatus(nightPushEnable, (success, error) => {
            if(success)
            {
                // 对夜间推送状态变更的服务器通信成功
            }
           else
           {
                // 夜间推送状态变更失败。请使用error.message显示错误消息。
            }
        });

一般/夜间/广告推送同时设置


如果是登录前需要确认是否允许推送的游戏,登录后必须调用以下代码。

  • 方式1

    GamePot.setPushStatus(bool pushEnable, bool nightPushEnable, bool adPushEnable);
    
    /// 对推送状态变更的服务器通信成功
    public void onPushStatusSuccess() {
    }
    
    /// 对推送状态变更的服务器通信失败
    public void onPushStatusFailure(NError error) {
    
        // 推送状态变更失败时,请使用弹窗等方式告知玩家error.message。
    }

  • 方式2

    void GamePot.setPushStatus(bool pushEnable, bool nightPushEnable, bool adPushEnable, GamePotCallbackDelegate.CB_Common);
    
    GamePot.setPushStatus(pushEnable, nightPushEnable, adPushEnable, (success, error) => {
        if(success)
        {
            // 对推送状态变更的服务器通信成功
        }
       else
       {
            // 推送状态变更失败。请使用弹窗等方式告知玩家error.message。
        }
    });

确认推送状态


如要确认当前推送状态,请使用下列代码。

NPushInfo pushInfo = GamePot.getPushStatus();

// 是否允许pushInfo.enable
// pushInfo.night   是否允许夜间推送

图片推送功能


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

  1. 在项目中点击 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
    // 继承GamePotNotificationServiceExtension代替UNNotificationServiceExtension
    // @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。

本地推送功能


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

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

int pushId = GamePot.sendLocalPush(DateTime.Parse("2018-01-01 00:00:00"), "title", "content");

// pushid的返回值由开发商管理 

取消已注册的本地推送


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

GamePot.cancelLocalPush(/*注册推送时获得的pushId*/);

Last updated