在活动中显示从Firebase发送的多个通知

时间:2018-07-24 03:29:01

标签: android firebase notifications firebase-cloud-messaging

我想显示已从Firebase发送的多个通知(我正在使用服务器,而不是php控制台),当应​​用程序关闭并再次打开时,它可以显示多个通知。但是我只收到一个通知,并且单击该通知时会显示最新消息,我希望这些通知保留在主要活动中。

主要活动

TextView text = (TextView) findViewById(R.id.data);
if (text != null) {
    Bundle extras = getIntent().getExtras();
    StringBuilder keys = new StringBuilder();
    if (extras != null) {
        for (String key : extras.keySet())
            keys.append(key + " = " + extras.getString(key) + "\n");
    }
    text.setText("" + keys.toString());
}

Firebase消息服务

 private void sendPushNotification(JSONObject json) {
    Log.e(TAG, "Notification JSON " + json.toString());
    try {
        JSONObject data = json.getJSONObject("data");

        String title = data.getString("title");
        String message = data.getString("message");
        String imageUrl = data.getString("image");

        MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());

        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("title",title);
        intent.putExtra("message",message);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0/*request code*/,intent,PendingIntent.FLAG_ONE_SHOT);

        if(imageUrl.equals("null")){
            mNotificationManager.showSmallNotification(title, message, intent);
        }
        else{
            mNotificationManager.showBigNotification(title, message, imageUrl, intent);
        }
    } 
    catch (JSONException e) {
        Log.e(TAG, "Json Exception: " + e.getMessage());
    } 
    catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
}

通知管理器

 public void showSmallNotification(String title, String message, Intent intent) {
    PendingIntent resultPendingIntent =
        PendingIntent.getActivity(
            mCtx,
            ID_SMALL_NOTIFICATION,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT
        );

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx,"channelId");
    Notification notification;
    notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
        .setAutoCancel(true)
        .setContentIntent(resultPendingIntent)
        .setContentTitle(title)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher))
        .setContentText(message)
        .setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
        .build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(ID_SMALL_NOTIFICATION, notification);
}

很抱歉,如果以前已经问过这个问题,但是我已经用它搜索了一下,没有找到我想要的任何类似情况,但这可能是由于我的词汇量有限。

1 个答案:

答案 0 :(得分:0)

好像是,您正在添加相同的ID_SMALL_NOTIFICATION。如果要显示多个通知,它应该每次都更改:

public void showSmallNotification(String title, String message, Intent intent) {
PendingIntent resultPendingIntent =
    PendingIntent.getActivity(
        mCtx,
        ID_SMALL_NOTIFICATION,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT
    );

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx,"channelId");
Notification notification;
notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
    .setAutoCancel(true)
    .setContentIntent(resultPendingIntent)
    .setContentTitle(title)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher))
    .setContentText(message)
    .setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
    .build();

notification.flags |= Notification.FLAG_AUTO_CANCEL;

NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(getNotificationId(), notification);}

使用getNotificationId()更改ID_SMALL_NOTIFICATION

private static int getNotificationId() {
    Random rnd = new Random();
    return 100 + rnd.nextInt(9000);
}