Firebase提供了两种放置推送通知的方法,一种是使用它的Firebase控制台,另一种是使用以前在GCM中使用的服务器。现在我们正在将我们的服务器从GCM转换为Firebase,我们需要在应用程序中接收并显示FCM通知。
我已经完成了firebase官方网站的实现,并且完成了示例代码,但是我仍然停留在这一部分上。当我们的服务器向Firebase发送推送通知,并且Firebase向我的应用程序发送通知时,我将以OnMessageReceived
方法接收通知,该方法带有RemoteMessage
参数。我知道消息在RemoteMessage参数内部,但我在Firebase官方文档中找不到任何关于如何处理这一点以显示android设备通知栏上的通知文本的指南或示例代码。
正如您在官方指南中所看到的,没有解释如何从RemoteMessage参数中提取没有问题的消息。我知道你可以在里面搜索并找到字符串,但我想找到一个关于如何正确和安全地处理这个的官方指南。
在正式文档中,您只有以下内容:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// 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());
// Handle message within 10 seconds
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
和这个(指南中没有调用它):
private void sendNotification(String messageBody) {
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);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_main)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
我已经做了类似的事情我正在使用addNotifcation()来代替sendNotification()
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getData().get("body"));
addNotification(remoteMessage.getData().get("body"), remoteMessage.getData().get("Nick"));
}
private void addNotification(String message, String title) {
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher1)
.setContentTitle(title)
.setContentText(message);
Intent notificationIntent = new Intent(this, app.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}