2

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());
    }
4

3 回答 3

2

我已经做了类似的事情,我正在使用 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());
}
于 2017-12-21T11:24:33.287 回答
0

您将从服务器收到一个 json onMessageRecieve 然后您需要解析该 json 并从中获取值。一旦您解析了值,您可以使用sendNotification()Method 发送它并显示您需要什么

于 2017-12-21T11:23:56.713 回答
0

检查下面的方法,它工作正常,我在通知中从服务器获取 json 数据。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        AppLog.Log(TAG, "From: " + remoteMessage.getFrom());
        JSONObject json = null;

        if (remoteMessage.getData().size() > 0) {
            Log.v(TAG, "Message data " + remoteMessage.getData());

            json = new JSONObject(remoteMessage.getData());
            AppLog.Log(TAG, "Json object " + json);

        }

        if (json != null) {

            try {
                int icon  = R.mipmap.ic_launcher;
                int mNotificationId = 001;
                // MainDrawerActivity is the class which open on the click of notification in status bar
                Intent intent = new Intent(this, MainDrawerActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

                //FLAG_UPDATE_CURRENT is important
                PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);


                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                        this);
                Notification notification = mBuilder.setSmallIcon(icon).setTicker("your title").setWhen(0)
                        .setAutoCancel(true)
                        .setContentTitle("your title")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("message"))
                        .setContentIntent(pendingIntent)
                        .setContentText("message").build();

                NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.notify(mNotificationId, notification);

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
于 2017-12-21T12:20:08.427 回答