我已经具有将通知发送到单个用户令牌的云功能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());
}
}