Токен Firebase Cloud Messaging генерируется до входа в систему.андроид - PullRequest
0 голосов
/ 20 сентября 2018

У меня уже есть 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());
    }




}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...