java - 推送通知前台和后台
问题描述
我创建了一个简单的通知,并使用创建和设计的全新图层为它设置了不同的颜色和一般样式。一旦我通过 Firebase 消息触发通知,当应用程序位于前台时,它可以完美地显示确切的样式我需要。
但是当应用程序在后台时,它使用它的默认样式。
无论如何要解决这个问题?谢谢。
通知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() 回调函数中如何处理数据消息和显示消息的信息。
https://firebase.google.com/docs/cloud-messaging/android/receive
推荐阅读
- ruby-on-rails - Rails 5.0.2 与 bundler 2.0.1 不兼容
- javascript - 为什么我班上的“口渴”方法不起作用?
- r - 连续x变量的ggplot直方图误差,
- android - EditText.text 返回的究竟是什么?
- javascript - 在 props 中调用回调函数
- flutter - 如何设置固定大小的背景渐变?
- angular - 对 Angular 应用程序的 Ajax 调用在现有路由上返回错误 404
- python - 如何用 Python 解决这个终端问题?
- javascript - npx webpack - 无法读取未定义的属性“缩小”
- java - 无法执行小程序程序(小程序未初始化)