android - 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());
}
}
解决方案
推荐阅读
- assembly - 如何确定 Intel x86 汇编代码中使用的初始寄存器值?
- c++ - 将结构传递为 void *
- pyspark - Pyspark:根据来自其他 pyspark 数据框的列名创建一个 pyspark 数据框
- bash - 是否有一个函数可以从两个文件的比较中提取行的唯一差异?
- c# - 如何读取 XML 文件并获取其中的所有 URL 链接并将它们保存到 TXT 文件?C#
- django - Django 文本选择行为
- regex - 捕获子组的多个实例
- node.js - 如果 Docker,图像节点:12.4 运行单个 js 文件时,“内部/模块/cjs/loader”中的错误
- c# - 用于在 asp .net 核心中绑定的复杂类型的转换器
- c# - MainWindow 中的按钮执行 UserControl1 中的方法