我试图发出触发令牌的通知。我还创建了onNewToken
,但它始终仅在我的应用程序首次安装时生成。因此,如果用户注销我的设备并使用另一个帐户登录,它将不会生成新令牌。
我已经知道文档中的这种引用
我的成就是,当用户在同一设备上注销时,它将在另一个用户登录时生成新令牌。
这是我的服务代码 公共类myFirebaseInstanceTokenID扩展了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);
SharedPreferences.Editor editor = getSharedPreferences("token_shared", MODE_PRIVATE).edit();
editor.putString("token", token);
editor.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");
if(user != null) {
sendNotificationNilai(judul_nilai, title_nilai);
}
Log.e(TAG, "body: " + judul_nilai);
Log.e(TAG, "title: " + title_nilai);
}
}
//for foreground process
if (remoteMessage.getNotification() != null) {
if(user != null){
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
else{
Log.d(TAG, "belum login : " + 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());
}
}