首页 > 解决方案 > Firebase 云消息传递令牌在登录前生成。安卓

问题描述

我已经有将通知发送到单个用户令牌的云功能 API。问题是当我生成令牌时,onNewToken它将在登录之前生成令牌,并且通知出现在用户登录之前。

我只想在用户登录和用户注销时存储 fcm 令牌。它将删除令牌

代码

public class myFirebaseInstanceTokenID extends FirebaseMessagingService {


    private static final String TAG = "FirebaseMessagingServce";
    private static final String TAG2 = "MyFirebaseIIDService";

    FirebaseAuth mAuth = FirebaseAuth.getInstance();
    FirebaseUser user = mAuth.getCurrentUser();


    @Override
    public void onNewToken(String token) {
        Log.d(TAG2, "Refreshed token: " + token);

        if(user != null){
            Log.d(TAG2, "user ada: " + token);
        }else{
            Log.d(TAG2, "user tidak ada: " + token);
            getSharedPreferences("_", MODE_PRIVATE).edit().putString("token", token).apply();
        }

    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {


        if (remoteMessage.getData().size() > 0) {
            Log.e(TAG, "Message data payload: " + remoteMessage.getData());
            Map<String, String> data = remoteMessage.getData();

            String data_type = data.get("data_type");
            Log.e(TAG, "data_type: " + data_type);

            if(data_type.equals("pengumuman")){
                String body_notif_pengumuman = data.get("body");
                String title_notif_pengumuman = data.get("title");
                sendNotificationPengumuman(body_notif_pengumuman, title_notif_pengumuman);
                Log.e(TAG, "body pengumuman: " + body_notif_pengumuman);
                Log.e(TAG, "title pengumuman: " + title_notif_pengumuman);
            }else if(data_type.equals("tugas")){
                Log.e(TAG, "masuk tugas: ");
                String body_notif = data.get("body");
                String title_notif = data.get("title");
                sendNotification(body_notif, title_notif);
                Log.e(TAG, "body: " + body_notif);
                Log.e(TAG, "title: " + title_notif);
            }else if(data_type.equals("nilai")){
                Log.e(TAG, "masuk nilai");
                String judul_nilai = data.get("judul");
                String title_nilai = data.get("title");
                sendNotificationNilai(judul_nilai, title_nilai);
                Log.e(TAG, "body: " + judul_nilai);
                Log.e(TAG, "title: " + title_nilai);
            }


        }


        //for foreground process
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }

    }

    private void sendNotification(String body_notif, String title_notif) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setAutoCancel(true)   //Automatically delete the notification
                .setSmallIcon(R.mipmap.ic_launcher) //Notification icon
                .setContentIntent(pendingIntent)
                .setContentTitle(body_notif)
                .setContentText(title_notif)
                .setVibrate(new long[] { 1000, 1000})
                .setSound(Settings.System.DEFAULT_NOTIFICATION_URI);


        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
        notificationManager.notify(m, notificationBuilder.build());
    }

    private void sendNotificationPengumuman(String body_notif, String title_notif) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setAutoCancel(true)   //Automatically delete the notification
                .setSmallIcon(R.mipmap.ic_launcher) //Notification icon
                .setContentIntent(pendingIntent)
                .setContentTitle(body_notif)
                .setContentText(title_notif)
                .setVibrate(new long[] { 1000, 1000})
                .setSound(Settings.System.DEFAULT_NOTIFICATION_URI);


        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
        notificationManager.notify(m, notificationBuilder.build());
    }


    private void sendNotificationNilai(String body_notif, String title_notif) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setAutoCancel(true)   //Automatically delete the notification
                .setSmallIcon(R.mipmap.ic_launcher) //Notification icon
                .setContentIntent(pendingIntent)
                .setContentTitle(body_notif)
                .setContentText(title_notif)
                .setVibrate(new long[] { 1000, 1000})
                .setSound(Settings.System.DEFAULT_NOTIFICATION_URI);


        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
        notificationManager.notify(m, notificationBuilder.build());
    }




}

标签: androidfirebasefirebase-cloud-messaging

解决方案


推荐阅读