猿问

我不能给 Token Firebase

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 通知。现在是调试模式。也许我必须更改它才能发布?请帮我。


慕森王
浏览 184回答 3
3回答

潇湘沐

According to latest docs,you can get new token id from onNewToken Method.<br/>&nbsp;@Override&nbsp; &nbsp; &nbsp; &nbsp; public void onNewToken(String s) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("NEW_TOKEN", s);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Implement your business logic to send your device token from here&nbsp; &nbsp; &nbsp; &nbsp; }

倚天杖

尝试更改您的 addOnSuccessListener 方法,删除第一个参数,如下所示:FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onSuccess(InstanceIdResult instanceIdResult) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Manage your token&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String token = instanceIdResult.getToken();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.i("FCM_TOKEN", token);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });

森栏

除了 FirebaseMessagingService 之外,还需要添加 FirebaseInstanceIdServicepublic class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {&nbsp; &nbsp; public MyFirebaseInstanceIDService() {&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onTokenRefresh() {&nbsp; &nbsp; &nbsp; &nbsp; // Get updated InstanceID token.&nbsp; &nbsp; &nbsp; &nbsp; String refreshedToken = FirebaseInstanceId.getInstance().getToken();&nbsp; &nbsp; &nbsp; &nbsp; MyNotificationManager.getInstance().registerToken(getApplicationContext(), refreshedToken);&nbsp; &nbsp; }}然后在您的清单中添加:&nbsp; &nbsp; <service&nbsp; &nbsp; &nbsp; &nbsp; android:name=".MyFirebaseInstanceIDService"&nbsp; &nbsp; &nbsp; &nbsp; android:enabled="true"&nbsp; &nbsp; &nbsp; &nbsp; android:exported="true"/>
随时随地看视频慕课网APP

相关分类

Java
我要回答