有时一个信号将 UserId 返回为 null [英] sometimes One signal returns UserId as null

查看:78
本文介绍了有时一个信号将 UserId 返回为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我通过一个信号向用户发送通知.但有时一个信号返回空用户 ID...那时我面临向用户发送通知的问题.我该如何解决.如果有人有解决方案,请与我分享

In my application i am sending notifications to the user through one signal.but sometimes one Signal returns null User Id...that time i am facing problem of sending notification to user.how can i resolve it. if anyone have solution, please share with me

这是我生成用户 ID 的代码

here is my code for generating user Id

    OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
            @Override
            public void idsAvailable(String userId, String registrationId) {
                Log.d("test","key is"+notification_key);
                notification_key=userId;
            }
        });

我正在 MyApplication.class 中像这样初始化 onesignal

and i am intialising onesignal like this in MyApplication.class

OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .unsubscribeWhenNotificationsAreDisabled(true)
                .init();

推荐答案

当用户接受推送通知权限时,他们不会立即拥有用户 ID.当用户接受推送通知权限时,SDK 会发出 HTTP 请求以注册 OneSignal 的服务器.该请求完成后,用户订阅状态将更新为用户 ID.

When the user accepts push notification permissions, they will not instantly have a user ID. When the user accepts push notification permissions, the SDK sends out an HTTP request to register with OneSignal's server. Once that request is finished, the user subscription state is updated with a user ID.

我建议使用 OSSubscriptionStateObserver 协议,以便在订阅状态发生变化时进行更新.您可以在那里访问用户 ID.

I would suggest using the OSSubscriptionStateObserver protocol in order to get updated whenever the subscription state changes. You can access the user ID there.

public class MainActivity extends Activity implements OSSubscriptionObserver {
  protected void onCreate(Bundle savedInstanceState) {
    OneSignal.addSubscriptionObserver(this);
  }
  
  public void onOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
    if (!stateChanges.getFrom().isSubscribed() &&
        stateChanges.getTo().isSubscribed()) {
         new AlertDialog.Builder(this)
             .setMessage("You've successfully subscribed to push notifications!")
             .show();
        // get player ID
        stateChanges.getTo().getUserId();
      }
   
      Log.i("Debug", "onOSPermissionChanged: " + stateChanges);
  }
}

这篇关于有时一个信号将 UserId 返回为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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