Android FCM - 如何只显示一个通知

时间:2017-04-25 12:41:58

标签: android json firebase push firebase-cloud-messaging

我将推送通知从FCM发送到Android设备,这是通过向包含JSON正文的FCM发送POST消息来完成的。

如果我发送两次相同的JSON主体,Android设备将显示两个通知(或三个或四个......)。但我希望它只显示一个。

" collapse_key"应该解决这个问题吧? (FCM Documentation

但是应该在哪里或如何插入?

这个SO问题回答了这个问题,但没有给出任何例子:Can FCM notification on Android overwrite previous one?

当前的JSON正文:

{
    "notification": {
        "title": "MyAPP",
        "body": "Open MyAPP to access your data",
        "click_action" : "OPEN_MAINACTIVITY",
        "icon": "ic_launcher_red",
        "color": "#ff0000"
    },
    "data": {
        "extra1":"sample1",
        "extra2":"sample2"
    },
    "registration_ids":[
        "--my_id--"
    ]
}

我尝试过很多方法来包含" collapse_key"但到目前为止没有运气。还有多个通知。欢迎任何帮助。

3 个答案:

答案 0 :(得分:3)

好吧,我一直在挖掘并找到答案:它不是" collapse_key",我应该使用"标签"通知中的选项。

因此,通过使用此JSON,只会显示一条通知:

{
    "notification": {
        "title": "MyAPP",
        "body": "Open MyAPP to access your data",
        "click_action" : "OPEN_MAINACTIVITY",
        "icon": "ic_launcher_red",
        "color": "#ff0000"
        "tag": "unique_tag"
    },
    "data": {
        "extra1":"sample1",
        "extra2":"sample2"
    },
    "registration_ids":[
        "--my_id--"
    ]
}

希望这有助于其他人!

如果有人希望进一步解释" collapse_key"我很高兴,显然我误解了它。

答案 1 :(得分:3)

“标签”选项是您要寻找的。如果新通知的标签与通知托盘中已显示的通知的标签相同,则新通知替换旧通知。

更新: 更新的FCM docs显示了如何将通用字段(所有平台通用)与特定于平台的参数分开。由于“标签”是android专用的选项,因此这是现在使其工作的方法:

{
   notification: {
       title: "Title Here",
       body: "Body Here",
   },
   //These are android-specific options to override
   android: {
       notification: {
           tag: "My-Tag"
       }
   },
   token: tokenHere
};

但是,这似乎引发了无效的JSON错误,并且无法识别“标签”字段。 这不起作用

{
   notification: {
       title: "Title Here",
       body: "Body Here",
       tag: "My-Tag"
   },
   token: tokenHere
}

答案 2 :(得分:0)

用于FCM的C#Rest API:

 WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
    tRequest.Method = "post";
    //serverKey - Key from Firebase cloud messaging server  
    tRequest.Headers.Add(string.Format("Authorization: key={0}", "AIXXXXXX...."));
    //Sender Id - From firebase project setting  
    tRequest.Headers.Add(string.Format("Sender: id={0}", "XXXXX.."));
    tRequest.ContentType = "application/json";
    var payload = new
    {
        to = "e8EHtMwqsZY:APA91bFUktufXdsDLdXXXXXX..........XXXXXXXXXXXXXX",
        priority = "high",
        content_available = true,
        notification = new
        {
            body = "Test",
            title = "Test",
            badge = 1
        },
    };

    string postbody = JsonConvert.SerializeObject(payload).ToString();
    Byte[] byteArray = Encoding.UTF8.GetBytes(postbody);
    tRequest.ContentLength = byteArray.Length;
    using (Stream dataStream = tRequest.GetRequestStream())
    {
        dataStream.Write(byteArray, 0, byteArray.Length);
        using (WebResponse tResponse = tRequest.GetResponse())
        {
            using (Stream dataStreamResponse = tResponse.GetResponseStream())
            {
                if (dataStreamResponse != null) using (StreamReader tReader = new StreamReader(dataStreamResponse))
                    {
                        String sResponseFromServer = tReader.ReadToEnd();
                        //result.Response = sResponseFromServer;
                    }
            }
        }
    }

android FCM通知接收代码

MyFirebaseInstanceIDService类

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

private static final String TAG = "MyFirebaseIIDService";

/**
 * Called if InstanceID token is updated. This may occur if the security of
 * the previous token had been compromised. Note that this is called when the InstanceID token
 * is initially generated so this is where you would retrieve the token.
 */
// [START refresh_token]
@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();

    Log.d(TAG, "Refreshed token: " + refreshedToken);

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.
    sendRegistrationToServer(refreshedToken);
}
// [END refresh_token]

/**
 * Persist token to third-party servers.
 *
 * Modify this method to associate the user's FCM InstanceID token with any server-side account
 * maintained by your application.
 *
 * @param token The new token.
 */
private void sendRegistrationToServer(String token) {
    // TODO: Implement this method to send token to your app server.
}}

MyFirebaseMessagingService类

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // [START_EXCLUDE]
    // There are two types of messages data messages and notification messages. Data messages are handled
    // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
    // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
    // is in the foreground. When the app is in the background an automatically generated notification is displayed.
    // When the user taps on the notification they are returned to the app. Messages containing both notification
    // and data payloads are treated as notification messages. The Firebase console always sends notification
    // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
    // [END_EXCLUDE]

    // 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());

        if (/* Check if data needs to be processed by long running job */ true) {
            // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
            scheduleJob();
        } else {
            // Handle message within 10 seconds
            handleNow();
        }

    }

    // 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.
}
// [END receive_message]


// [START on_new_token]
/**
 * Called if InstanceID token is updated. This may occur if the security of
 * the previous token had been compromised. Note that this is called when the InstanceID token
 * is initially generated so this is where you would retrieve the token.
 */
@Override
public void onNewToken(String token) {
    Log.d(TAG, "Refreshed token: " + token);

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.
    sendRegistrationToServer(token);
}
// [END on_new_token]

/**
 * Schedule a job using FirebaseJobDispatcher.
 */
private void scheduleJob() {
    // [START dispatch_job]
    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
    Job myJob = dispatcher.newJobBuilder()
            .setService(MyJobService.class)
            .setTag("my-job-tag")
            .build();
    dispatcher.schedule(myJob);
    // [END dispatch_job]
}

/**
 * Handle time allotted to BroadcastReceivers.
 */
private void handleNow() {
    Log.d(TAG, "Short lived task is done.");
}

/**
 * Persist token to third-party servers.
 *
 * Modify this method to associate the user's FCM InstanceID token with any server-side account
 * maintained by your application.
 *
 * @param token The new token.
 */
private void sendRegistrationToServer(String token) {
    // TODO: Implement this method to send token to your app server.
}

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 */
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);

    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.ic_stat_ic_notification)
            .setContentTitle("FCM Message")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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