【发布时间】:2020-02-11 09:35:10
【问题描述】:
我正在发送仅包含来自 Firebase 云消息传递 HTTP 协议的数据负载的通知。我可以在我的 Android 应用程序中的“FirebaseMessagingService”的“onMessageReceived”回调方法中看到通知正确到达。问题是我无法从这里生成通知。我已经编写了用于生成通知的代码,但通知没有出现在手机的通知区域中。
下面是我的代码:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String data = remoteMessage.getData().toString();
Log.d("<<<>>>", "MyFirebaseMessagingService > onMessageReceived > data : " + data);
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
showNotification(title, body);
}
private void showNotification(String title, String body) {
Log.d("<<<>>>", "MyFirebaseMessagingService > showNotification() called !");
Log.d("<<<>>>", "title : " + title);
Log.d("<<<>>>", "body : " + body);
Intent intent = new Intent(this, SplashScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "NewsVault_Notification_Channel")
.setSmallIcon(R.drawable.firebase_notification_icon)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
下面是我的 Logcat 输出:
D/<<<>>>: MyFirebaseMessagingService > onMessageReceived > data :
{channel_id=NewsVault_Notification_Channel, priority=high, body=Test body, key_1=Value for key_1,
key_2=Value for key_2, title=Test title, content_available=true}
D/<<<>>>: MyFirebaseMessagingService > showNotification() called !
D/<<<>>>: title : Test title
D/<<<>>>: body : Test body
我在 Manifest 文件中添加了以下内容:
<service
android:name=".Services.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="NewsVault_Notification_Channel" />
【问题讨论】:
标签: android push-notification firebase-cloud-messaging android-notifications