gpt4 book ai didi

java - 如何使用 Firebase Cloud Messaging 在前台显示多个通知

转载 作者:行者123 更新时间:2023-12-02 00:08:21 25 4
gpt4 key购买 nike

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

公共(public)类 FirebaseNotifications 扩展了 FirebaseMessagingService{ 私有(private)静态最终字符串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;
}
}

}

最佳答案

我决定了。

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

关于java - 如何使用 Firebase Cloud Messaging 在前台显示多个通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58140660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com