当前位置: 首页 > 知识库问答 >
问题:

如何使用Firebase云消息在前台显示多个通知

姬向明
2023-03-14

我在Android中有一个应用程序,它接收来自firebase的通知,但当它在前台时,新的通知会取代以前的通知。我怎样才能全部展示出来?这是我的课。

公共类FirebaseNotifications扩展了FirebaseMessagingService{private static final String TAG=“MyFirebaseMsgService”;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // [START_EXCLUDE]
    // There are two types of messages data messages and notification messages. Data messages
    // are handled
    // here in onMessageReceived whether the app is in the foreground or background. Data
    // messages are the type
    // traditionally used with GCM. Notification messages are only received here in
    // onMessageReceived when the app
    // is in the foreground. When the app is in the background an automatically generated
    // notification is displayed.
    // When the user taps on the notification they are returned to the app. Messages
    // containing both notification
    // and data payloads are treated as notification messages. The Firebase console always
    // sends notification
    // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
    // [END_EXCLUDE]

    // TODO(developer): Handle FCM messages here.
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

        if (/* Check if data needs to be processed by long running job */ true) {
            // For long-running tasks (10 seconds or more) use WorkManager.
            scheduleJob();

        } else {
            // Handle message within 10 seconds
            handleNow();

        }
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        //message will contain the Push Message
        String message = remoteMessage.getNotification().getBody();
        //imageUri will contain URL of the image to be displayed with Notification
        String imageUri = remoteMessage.getNotification().getIcon();

        //To get a Bitmap image from the URL received

        sendNotification(message, imageUri);
    }

    //The message which i send will have keys named [message, image, AnotherActivity] and corresponding values.
    //You can change as per the requirement.



}

@Override
public void onNewToken(String token) {
    Log.d(TAG, "Refreshed token: " + token);

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.
    sendRegistrationToServer(token);
}

private void scheduleJob() {
    // [START dispatch_job]
    //OneTimeWorkRequest work = new OneTimeWorkRequest.Builder(MyWorker.class)
    //        .build();
    //WorkManager.getInstance().beginWith(work).enqueue();
    // [END dispatch_job]
}

private void handleNow() {
    Log.d(TAG, "Short lived task is done.");
}

private void sendRegistrationToServer(String token)
{
    // TODO: Implement this method to send token to your app server.
}

private void sendNotification(String messageBody, String icon)
{

    String channelId = "General";
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setLargeIcon(getBitmapFromURL(icon))
                    .setContentTitle("Chapur")
                    .setSmallIcon(R.drawable.icon_agenda)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
}

public Bitmap getBitmapFromURL(String strURL) {
    try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection)
                url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

}

共有2个答案

桓修能
2023-03-14

你的问题是你总是在行中使用“0”作为你的通知ID

通知anager.notify(0,通知Builder.build());

如果你每次给它一个新的id,你就不会有这个问题:)

宋高扬
2023-03-14

我决心。

private final static AtomicInteger c = new AtomicInteger(0);

    public static int getID() {
        return c.incrementAndGet();
    }


private void sendNotification(String messageBody, String icon)
{

    String channelId = "General";
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setLargeIcon(getBitmapFromURL(icon))
                    .setContentTitle("Chapur")
                    .setSmallIcon(R.drawable.icon_agenda)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(getID(), notificationBuilder.build());
}
 类似资料:
  • 我正在使用FCM为我的iOS应用程序创建和发送推送通知。 开发环境: > Xcode 11.3 null 问题: 在遇到问题之前,我设置了我的应用程序,使其能够在应用程序处于后台和前台时接收通知。我对自己非常高兴,我promise了代码。在此之后,我无法在前台或后台接收通知。无论使用的通知是从云消息仪表板还是邮递员发送的,我都收到一个成功的响应但通知从未出现。 起初,我认为我可能已经达到通知配额,

  • 我开始使用新的Google通知服务 。 多亏了这个代码https://github.com/Firebase/quickstart-android/Tree/master/messaging,我才能够从我的Firebase用户控制台向我的Android设备发送通知。 是否有任何API或方法可以在不使用Firebase控制台的情况下发送通知?我的意思是,例如,一个PHP API或类似的东西,从我自己

  • 我正在尝试从OneSignal移动到Firebase云消息。我遇到的问题是,我无法在有效负载中定义键值对来本地化通知。我必须在我的应用程序中的字符串文件中定义一个键值对。这意味着,如果我想发送一个字符串文件中未定义的完全不同的通知,则该透视图中的通知不能是动态的。 当使用OneSignal时,我可以像这样定义用于本地化的有效负载: 这对云消息传递也是可能的吗?

  • 使用更新的Firebase cloud messaging和swizzling方法,当我的应用程序前景化时,我能够成功地在我的应用程序委托中的方法中接收有效负载。但是,我没有收到任何类型的有效负载,并且当我的应用程序处于后台时,不会被调用,尽管api响应消息已成功发送(见下文) 以下是我发送给FCM api的请求,以触发推送通知 有FCM的回复 这是我的appDelegate代码 我在我的应用程序

  • 我正在使用Firebase云消息向我的android设备发送通知。在我的项目控制台的“报告”部分,我收到了0条消息,发送了100条消息。问题是,我实际上在我的设备上收到了通知,但为什么它们没有出现在云消息报告中呢? 我该如何解决这个问题呢?