在Android中获取多个通知,而不是一个 [英] Getting Multiple Notification instead of one in Android

查看:260
本文介绍了在Android中获取多个通知,而不是一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Firebase notification in my android app. Generating notification using below code :

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();
}
}

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

The issue is multiple notification on status bar is generating instead of single, for the Same notification sent from the backend.

我在我的常量文件中将NOTIFICATION_ID用作静态整数. 而我正在增加它以进行不同的通知.

i have taken NOTIFICATION_ID as a static integer in my constant file. and i am incrementing it for differnt notification.

可能是什么问题?

推荐答案

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

do not increase notification id when u receive same notification. you need to set some flag to check if the notification received is the same or different notification. when you send notification from FCM send some extra payload which has key-value pair which bears the notification id. set that notification ID instead of increasing from your code. Hope it helps

这篇关于在Android中获取多个通知,而不是一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆