在我的應用程序中成功實施Firebase以發送有關我的博客的推送通知,但現在我想知道用戶如何「關閉」我發送給應用程序的通知,這裏是我的代碼:在我的應用程序中關閉推送通知
package com.lfcchile;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
/**
* Created by Jona on 7/23/16.
*/
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO: Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_menu_destacado)
.setContentTitle("LFC Chile")
.setContentText(remoteMessage.getNotification().getBody())
.setTicker("Ticker")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
想法請好嗎?提前致謝!!
如何設置一個pref,並忽略推動如果想要的。 – lionscribe
關於添加一個跟蹤用戶是否想要通知的共享pref的建議是公平的。但請記住,這僅適用於FCM數據消息。如果您在應用處於後臺時發送通知消息(如來自Firebase控制檯的通知消息),則會自動顯示通知。 –