2016-07-25 96 views
1

在我的應用程序中成功實施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()); 

    } 

} 

想法請好嗎?提前致謝!!

+0

如何設置一個pref,並忽略推動如果想要的。 – lionscribe

+0

關於添加一個跟蹤用戶是否想要通知的共享pref的建議是公平的。但請記住,這僅適用於FCM數據消息。如果您在應用處於後臺時發送通知消息(如來自Firebase控制檯的通知消息),則會自動顯示通知。 –

回答

1

您可以添加某種共享優先級,並存儲來自用戶的值。然後在獲取通知後獲取值,如果用戶的值爲true,則跳過創建通知。

+0

下面給出了更好的觀點! –

+0

雖然這聽起來像是會起作用,但是這不會不必要地損耗電池嗎? – jobbert

+0

根本沒有,儘管您也可以檢索節點中的用戶令牌並使用Firebase功能或服務器搜索該節點中用戶的令牌,並且如果它存在不發送推送通知,則會更慢,成本的錢,這就是爲什麼共享偏好選項是最好的 – Andrea

1

您可以爲自己的偏好設置一個切換按鈕,然後在執行代碼之前檢查切換的值。因此,它變得像if-else語句一樣簡單。

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); 
     if(this.isEnabled == true){ // if statement added here 
      notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
     } 
    } 

} 

這一個將工作!確保isEnebled變量是全局的。

+0

這不適用於我的情況可以請你幫我 – PriyankaChauhan

相關問題