当我从Firebase发送推送消息时,如果应用程序在后台或关闭状态下,我会收到通知,但是当应用程序处于打开状态时,通知不显示...
调试后我发现停留在MyMessagingService的onMessageReceived方法上,所以我猜测我的问题可能与生成通知(或者随通知一起发送的意图)有关。
我已经实现了这个服务:
public class MyMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyMessagingService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
Log.d(TAG, "From: " + remoteMessage.getFrom());
if(remoteMessage!=null)
{
String id = null;
String title = null;
String message = null;
String launchPage = null;
if(remoteMessage.getNotification()!=null)
{
if(remoteMessage.getNotification().getTitle()!=null)
{
title = remoteMessage.getNotification().getTitle();
}
if(remoteMessage.getNotification().getBody()!=null)
{
message = remoteMessage.getNotification().getBody();
}
}
if(remoteMessage.getMessageId()!=null)
{
id = remoteMessage.getMessageId();
}
Log.e(TAG, "id: " + id);
Log.e(TAG, Consts.TITLE + title);
Log.e(TAG, Consts.MESSAGE + ": " + message);
// Check if message contains a data payload.
if (remoteMessage.getData()!=null && remoteMessage.getData().size() > 0)
{
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if(remoteMessage.getData().containsKey(Consts.LAUNCH_PAGE))
{
launchPage = remoteMessage.getData().get(Consts.LAUNCH_PAGE);
}
Log.e(TAG, Consts.LAUNCH_PAGE + ": " + launchPage);
sendNotification(title, message, launchPage);
}
}
}
// [END receive_message]
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String title, String messageBody, String page)
{
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(Consts.TITLE, title);
intent.putExtra(Consts.MESSAGE, messageBody);
if (launchPage != null)
{
intent.putExtra(Consts.LAUNCH_PAGE, launchPage);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.status)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
} }
在我的清单文件(manifest)中,我有:
<!-- [START fcm_default_icon] -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/status" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/accent" />
<!-- [END fcm_default_icon] -->
<!-- [START fcm_default_channel] -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/>
<!-- [END fcm_default_channel] -->
<service
android:name=".controller.service.MyMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".controller.service.MyInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
有什么建议可以在应用程序在前台时生成通知吗?