【问题标题】:Display multiple notifications that have been sent from Firebase on activity显示多个从 Firebase 发送的关于活动的通知
【发布时间】:2018-07-24 03:29:00
【问题描述】:

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

主要活动

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

很抱歉,如果之前已经问过这个问题,但我已经用谷歌搜索过,没有找到任何我想要的类似案例,但这可能是由于我的词汇量有限。

【问题讨论】:

    标签: android firebase notifications firebase-cloud-messaging


    【解决方案1】:

    似乎您正在添加相同的 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);
    }
    

    【讨论】:

    • 是的,谢谢你的工作,但是当我关闭应用程序时如何让消息保持不变?我希望活动显示所有已发送到应用程序的通知
    • 在这种情况下,您需要将数据存储在数据库中,当活动将打开从数据库中获取数据并显示在那里时
    • 我存储的数据是 json 数据还是附加数据?对不起,我对这个 android 编程真的很陌生
    • 通知附带的任何数据以及您要显示的任何数据都将它们存储在数据库中
    猜你喜欢
    • 2023-03-04
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 2019-11-11
    相关资源
    最近更新 更多