我有一个信使应用程序,现在我正在做推送通知部分。我正在使用FCM
(Firebase云消息传递)。现在我有一个问题。例如,介绍2个用户(user1和user2)。如果user1给我写信,我会收到FCM
的通知并显示它。当user2写信给我时,我正在为它创建新的通知。但是当user1再次写信给我并且我没有删除我的user1通知时,我需要更新通知,否则创建新通知。我想你了解我。现在我被困在更新部分,因为每次我都在创建一个关于消息收入的新通知。
我搜索并找到了一些东西。它说我必须使用NotificationListenerService
,并使用getActiveNotifications()
获取状态栏中的所有活动通知。
所以我尝试了一些东西,但我得到了null
。我在Notification Access中启用了这个应用程序,所以我认为这不是问题所在。我在清单文件中注册了我的服务。
这是我的代码,我创建了FCM
服务。如果我在代码中做错了,请修复它。
public class FireBaseService extends FirebaseMessagingService {
private static final String TAG = "FireBaseMessagingServiceTag";
@SuppressLint("LongLogTag")
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Handle FCM messages here.
Intent service = new Intent(FireBaseService.this, NotificationService.class);
startService(service);
//I think I'm doing wrong here.
NotificationService notificationService = new NotificationService();
StatusBarNotification[] activeNotifications = notificationService.getActiveNotifications();
Log.d(TAG, "Array: " + Arrays.toString(activeNotifications));
//show notification
}
}
这是NotificationService
类。
public class NotificationService extends NotificationListenerService {
@Override
public IBinder onBind(Intent intent) {
return super.onBind(intent);
}
@Override
public void onListenerConnected() {
super.onListenerConnected();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn){
// Implement what you want here
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn){
// Implement what you want here
}
@Override
public StatusBarNotification[] getActiveNotifications() {
return super.getActiveNotifications();
}
}
所以这就是我所做的一切。你能帮助我吗?谢谢你,请修改我的代码,如果我做错了什么而不是对我的问题投反对票。
这是通知部分。
private void showNotification(String title, String body) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "com.example.fcmnotification.FireBaseService";
if(notificationManager != null) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID, "NotificationManager",
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("My Channel");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher_notification)
.setContentTitle(title)
.setContentText(body)
.setContentInfo("info");
notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
}
}
你不需要NotificationListenerService
。只需从NotificationManager
获取有效通知
NotificationManager nm = (NotificationManager)ctx.getSystemService(Activity.NOTIFICATION_SERVICE);
StatusBarNotification[] statusNotifs = null;
try {
statusNotifs = nm.getActiveNotifications();
} catch (Throwable t) {
// it can crash
}
if (statusNotifs != null) for (StatusBarNotification n : statusNotifs) {
Notification notif = n.getNotification();
try {
String ntext = notif.extras.getCharSequence(Notification.EXTRA_TEXT).toString();
// do something
} catch (Throwable t) {
// can crash
}
}