为了账号安全,请及时绑定邮箱和手机立即绑定

我不能给 Token Firebase

我不能给 Token Firebase

慕森王 2021-11-11 18:06:44
MyFirebaseMessagingService.java:public class MyFirebaseMessagingService extends FirebaseMessagingService {    @Override    public void onNewToken(String s) {        Log.e("NEW_TOKEN", s);    }    @Override    public void onMessageReceived(RemoteMessage remoteMessage) {        Map<String, String> params = remoteMessage.getData();        JSONObject object = new JSONObject(params);        Log.e("JSON_OBJECT", object.toString());        String NOTIFICATION_CHANNEL_ID = "Nilesh_channel";        long pattern[] = {0, 1000, 500, 1000};        NotificationManager mNotificationManager =                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications",                    NotificationManager.IMPORTANCE_HIGH);            notificationChannel.setDescription("");            notificationChannel.enableLights(true);            notificationChannel.setLightColor(Color.RED);            notificationChannel.setVibrationPattern(pattern);            notificationChannel.enableVibration(true);            mNotificationManager.createNotificationChannel(notificationChannel);        }        // to diaplay notification in DND Mode        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);            channel.canBypassDnd();        }        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);        mNotificationManager.notify(1000, notificationBuilder.build());    }}跳过MainActivity 中的此代码。我不知道该怎么办。Firebase OAuth 完美运行。但是没有令牌,我无法检查 Firebase 通知。现在是调试模式。也许我必须更改它才能发布?请帮我。
查看完整描述

3 回答

?
潇湘沐

TA贡献1816条经验 获得超6个赞

According to latest docs,you can get new token id from onNewToken Method.<br/>


 @Override

        public void onNewToken(String s) {

            Log.e("NEW_TOKEN", s);

            //Implement your business logic to send your device token from here

        }


查看完整回答
反对 回复 2021-11-11
?
倚天杖

TA贡献1828条经验 获得超3个赞

尝试更改您的 addOnSuccessListener 方法,删除第一个参数,如下所示:


FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {

        @Override

        public void onSuccess(InstanceIdResult instanceIdResult) {

            //Manage your token

            String token = instanceIdResult.getToken();

            Log.i("FCM_TOKEN", token);


        }

    });


查看完整回答
反对 回复 2021-11-11
?
森栏

TA贡献1810条经验 获得超5个赞

除了 FirebaseMessagingService 之外,还需要添加 FirebaseInstanceIdService


public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {


    public MyFirebaseInstanceIDService() {


    }


    @Override

    public void onTokenRefresh() {

        // Get updated InstanceID token.

        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        MyNotificationManager.getInstance().registerToken(getApplicationContext(), refreshedToken);

    }

}

然后在您的清单中添加:


    <service

        android:name=".MyFirebaseInstanceIDService"

        android:enabled="true"

        android:exported="true"/>


查看完整回答
反对 回复 2021-11-11
  • 3 回答
  • 0 关注
  • 179 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信