簡體   English   中英

推送通知的前景和背景

[英]Push notification foreground and background

我創建了一個簡單的通知,並使用全新創建和設計的圖層使用不同的顏色和常規樣式對它進行了樣式設置。通過Firebase Messages觸發通知后,當應用程序位於前台時,它將完美顯示確切的樣式我需要。

但是,當應用程序在后台運行時,它使用的是丑陋的默認樣式。

無論如何要解決這個問題? 謝謝。

通知Java類文件代碼-

public class MyFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

private static final String TAG = "FirebaseMessagingServic";

public MyFirebaseMessagingService() {
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    String title = remoteMessage.getNotification().getTitle();
    String message = remoteMessage.getNotification().getBody();
    Log.d(TAG, "onMessageReceived: Message Received! \n " +
        "Title : " + title + "\n" +
            "Message : " + message);

    sendNotification(title, message);

}

@Override
public void onDeletedMessages() {



}

private void sendNotification(String title, String messageBody) {

    final RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_layout);

    remoteViews.setTextViewText(R.id.remoteview_notification_short_message, messageBody);
    remoteViews.setTextViewText(R.id.notificationDate, title);

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = "0";
    Uri defaultSoundUri= Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/notice");
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)

                    .setSmallIcon(R.mipmap.minilogored)
                    .setContentIntent(pendingIntent)
                    .setContent(remoteViews)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setLights(0xf14d39, 1500, 2000)
                    .setPriority(NotificationCompat.PRIORITY_HIGH);


    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

}

請參考鏈接以了解有關onMessageReceived()回調函數中如何處理數據消息和顯示消息的更多信息。

當Firebase中的應用程序在后台運行時如何處理通知

https://firebase.google.com/docs/cloud-messaging/android/receive

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM