Android Firebase云消息传递令牌在登录前生成。安卓

Android Firebase云消息传递令牌在登录前生成。安卓,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,我已经有了将通知发送到单用户令牌的云函数API。 问题是,当我使用onNewToken生成令牌时,这将在登录之前生成令牌,并且在用户登录之前显示通知 我只想在用户登录和注销时存储fcm令牌。它将删除令牌 代码 公共类myFirebaseInstanceTokenID扩展了FirebaseMessagingService{ 私有静态最终字符串TAG=“FirebaseMessagingServce”; 私有静态最终字符串TAG2=“MyFirebaseIIDService”; FirebaseAu

我已经有了将通知发送到单用户令牌的云函数API。 问题是,当我使用
onNewToken
生成令牌时,这将在登录之前生成令牌,并且在用户登录之前显示通知

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

代码

公共类myFirebaseInstanceTokenID扩展了FirebaseMessagingService{
私有静态最终字符串TAG=“FirebaseMessagingServce”;
私有静态最终字符串TAG2=“MyFirebaseIIDService”;
FirebaseAuth mAuth=FirebaseAuth.getInstance();
FirebaseUser=mAuth.getCurrentUser();
@凌驾
公共void onNewToken(字符串标记){
Log.d(TAG2,“刷新的令牌:“+token”);
如果(用户!=null){
Log.d(TAG2,“用户ada::+token”);
}否则{
Log.d(TAG2,“用户tidak ada:”+令牌);
GetSharedReferences(“\u”,MODE\u PRIVATE).edit().putString(“token”,token).apply();
}
}
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage){
如果(remoteMessage.getData().size()>0){
Log.e(标记,“消息数据负载:”+remoteMessage.getData());
Map data=remoteMessage.getData();
字符串数据类型=data.get(“数据类型”);
Log.e(标签,“数据类型:”+数据类型);
if(数据类型等于(“pengumuman”)){
字符串body_notif_pengumuman=data.get(“body”);
字符串title_notif_pengumuman=data.get(“title”);
发送通知人(机构、头衔);
Log.e(标签“body pengumuman:+body_notif_pengumuman”);
Log.e(标签“标题pengumuman:+标题notif_pengumuman”);
}else if(数据类型等于(“tugas”)){
Log.e(标签“masuk tugas:”);
字符串body_notif=data.get(“body”);
字符串title_notif=data.get(“title”);
发送通知(正文、标题);
Log.e(标签,“主体:+body_notif”);
日志e(标签,“标题:”+标题不适用);
}else if(数据类型等于(“nilai”)){
Log.e(标签“masuk nilai”);
字符串judul_nilai=data.get(“judul”);
字符串title_nilai=data.get(“title”);
sendNotificationNilai(judul_nilai,title_nilai);
Log.e(标签,“主体:+judul_nilai”);
Log.e(标签,“标题:”+title_nilai);
}
}
//用于前台进程
if(remoteMessage.getNotification()!=null){
Log.d(标记,“消息通知正文:”+remoteMessage.getNotification().getBody());
}
}
私有void sendNotification(字符串正文\u notif、字符串标题\u notif){
意向意向=新意向(此,MainActivity.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivity(this,0,intent,
悬挂式帐篷(一杆旗帜);
NotificationCompat.Builder notificationBuilder=(NotificationCompat.Builder)新建NotificationCompat.Builder(此)
.setAutoCancel(true)//自动删除通知
.setSmallIcon(R.mipmap.ic_启动器)//通知图标
.setContentIntent(挂起内容)
.setContentTitle(正文\u notif)
.setContentText(标题\u notif)
.setVibrate(新长[]{1000,1000})
.setSound(Settings.System.DEFAULT\u通知\u URI);
NotificationManager NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
int m=(int)((new Date().getTime()/1000L)%Integer.MAX_值);
notificationManager.notify(m,notificationBuilder.build());
}
私有void sendNotificationPengumuman(字符串正文\u notif,字符串标题\u notif){
意向意向=新意向(此,MainActivity.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivity(this,0,intent,
悬挂式帐篷(一杆旗帜);
NotificationCompat.Builder notificationBuilder=(NotificationCompat.Builder)新建NotificationCompat.Builder(此)
.setAutoCancel(true)//自动删除通知
.setSmallIcon(R.mipmap.ic_启动器)//通知图标
.setContentIntent(挂起内容)
.setContentTitle(正文\u notif)
.setContentText(标题\u notif)
.setVibrate(新长[]{1000,1000})
.setSound(Settings.System.DEFAULT\u通知\u URI);
NotificationManager NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
int m=(int)((new Date().getTime()/1000L)%Integer.MAX_值);
notificationManager.notify(m,notificationBuilder.build());
}
私有void sendNotificationNilai(字符串正文\u notif,字符串标题\u notif){
意向意向=新意向(此,MainActivity.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivity(this,0,intent,
悬挂式帐篷(一杆旗帜);
NotificationCompat.Builder notificationBuilder=(NotificationCompat.Builder)新建NotificationCompat.Builder(此)
.setAutoCancel(true)//自动删除通知
.setSmallIcon(R.mipmap.ic_启动器)//通知图标
.setContentIntent(挂起内容)
.setContentTitle(正文\u notif)
.setContentText(标题\u notif)
.setVibrate(新长[]{1000,1000})
.setSound(Settings.System.DEFAULT\u通知\u URI);
NotificationManager通知
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());
    }




}