FirebaseInstanceIdService is deprecated

后端 未结 10 2345

Hope all of you aware of this class, used to get notification token whenever firebase notification token got refreshed we get the refreshed token from this class, From follo

10条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 10:23

    Yes FirebaseInstanceIdService is deprecated

    FROM DOCS :- This class was deprecated. In favour of overriding onNewToken in FirebaseMessagingService. Once that has been implemented, this service can be safely removed.

    No need to use FirebaseInstanceIdService service to get FCM token You can safely remove FirebaseInstanceIdService service

    Now we need to @Override onNewToken get Token in FirebaseMessagingService

    SAMPLE CODE

    public class MyFirebaseMessagingService extends FirebaseMessagingService {
    
        @Override
        public void onNewToken(String s) {
            Log.e("NEW_TOKEN", s);
        }
    
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
    
            Map 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);
    
            notificationBuilder.setAutoCancel(true)
                    .setColor(ContextCompat.getColor(this, R.color.colorAccent))
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(remoteMessage.getNotification().getBody())
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setAutoCancel(true);
    
    
            mNotificationManager.notify(1000, notificationBuilder.build());
        }
    }
    

    EDIT

    You need to register your FirebaseMessagingService in manifest file like this

        
            
    
                
            
        
    

    how to get token in your activity

    .getToken(); is also deprecated if you need to get token in your activity than Use getInstanceId ()

    Now we need to use getInstanceId () to generate token

    getInstanceId () Returns the ID and automatically generated token for this Firebase project.

    This generates an Instance ID if it does not exist yet, which starts periodically sending information to the Firebase backend.

    Returns

    • Task which you can use to see the result via the InstanceIdResult which holds the ID and token.

    SAMPLE CODE

    FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this,  new OnSuccessListener() {
         @Override
         public void onSuccess(InstanceIdResult instanceIdResult) {
               String newToken = instanceIdResult.getToken();
               Log.e("newToken",newToken);
    
         }
     });
    

    EDIT 2

    Here is the working code for kotlin

    class MyFirebaseMessagingService : FirebaseMessagingService() {
    
        override fun onNewToken(p0: String?) {
    
        }
    
        override fun onMessageReceived(remoteMessage: RemoteMessage?) {
    
    
            val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            val NOTIFICATION_CHANNEL_ID = "Nilesh_channel"
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val notificationChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications", NotificationManager.IMPORTANCE_HIGH)
    
                notificationChannel.description = "Description"
                notificationChannel.enableLights(true)
                notificationChannel.lightColor = Color.RED
                notificationChannel.vibrationPattern = longArrayOf(0, 1000, 500, 1000)
                notificationChannel.enableVibration(true)
                notificationManager.createNotificationChannel(notificationChannel)
            }
    
            // to diaplay notification in DND Mode
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID)
                channel.canBypassDnd()
            }
    
            val notificationBuilder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
    
            notificationBuilder.setAutoCancel(true)
                    .setColor(ContextCompat.getColor(this, R.color.colorAccent))
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(remoteMessage!!.getNotification()!!.getBody())
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setAutoCancel(true)
    
    
            notificationManager.notify(1000, notificationBuilder.build())
    
        }
    }
    

提交回复
热议问题