【问题标题】:Getting Multiple Notification instead of one in Android在Android中获得多个通知而不是一个
【发布时间】:2017-08-18 13:37:26
【问题描述】:

我在我的 Android 应用中使用 Firebase 通知。 使用以下代码生成通知:

public class FirebaseNotificationService extends FirebaseMessagingService {

private static final String TAG = FirebaseNotificationService.class.getSimpleName();
private String strTitle = "My Notification";
private String strMessage = "No Description";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (Prefrences.getBooleanValue(getApplicationContext(), IS_LOGIN) && Prefrences.getBooleanValue(getApplicationContext(), Prefrences.IS_NOTIFICATION)) {
        if (remoteMessage.getData().get("title") != null) {
            strTitle = remoteMessage.getData().get("title");
        }
        if (remoteMessage.getData().get("message") != null) {
            strMessage = remoteMessage.getData().get("message");
        }
        sendNotification(strTitle, strMessage);
    }
}

private void sendNotification(String strTitle, String messageBody) {
    try {
        if (!getTopActivity().equals("com.app.activity.NotificationActivity")) {
            Intent intent = new Intent(this, DashboardActivity.class);
            intent.putExtra("notification", "yes");
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);

            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.app_icon)
                    .setContentTitle(strTitle)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            notificationManager.notify(++NOTIFICATION_ID /* ID of notification */, notificationBuilder.build());
        } else {
            Intent intent = new Intent();
            intent.setAction("load_service");
            sendBroadcast(intent);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public String getTopActivity() {
    ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    return taskInfo.get(0).topActivity.getClassName();
}
}

问题是状态栏上的多个通知正在生成而不是单个通知,对于从后端发送的相同通知。

我已将 NOTIFICATION_ID 作为常量文件中的静态整数。 我正在为不同的通知增加它。

可能是什么问题?

【问题讨论】:

  • 我假设,如果多次收到相同的通知,则只显示一个。但不同类型的通知接收然后显示多个。对吗?

标签: android firebase push-notification android-pendingintent firebase-notifications


【解决方案1】:

当你收到相同的通知时,不要增加通知 ID。您需要设置一些标志来检查收到的通知是相同还是不同的通知。当您从 FCM 发送通知时,发送一些额外的有效负载,其中包含带有通知 ID 的键值对。设置该通知 ID,而不是从您的代码中增加。希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    相关资源
    最近更新 更多