3

我已經在我的應用程序中通過Firebase實施了推送通知。即使通知已從設置中禁用,通知即將到來。 我爲火力地堡實施的類別是:通過應用程序設置中的切換按鈕啓用或禁用FCM推送通知

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

      private static final String TAG = "MyFirebaseMsgService"; 
      @Override 
      public void onMessageReceived(RemoteMessage remoteMessage) { 

       //Displaying data in log 

       Log.e(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
       //Calling method to generate notification 

       String to=""; 
       to = remoteMessage.getData().get("key1"); 

     //when the notification is disabled then also the notification is coming 
    if(notification_enable) { 
    sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody(),to); 
      } 
     } 

      //This method is only generating push notification 
      //It is same as we did in earlier posts 
      private void sendNotification(String title,String messageBody,String to) { 
        Intent intent = new Intent(this, Splash_Activity.class); 
        intent.putExtra("key1",to); 
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 


       PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 

       Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
       NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
         .setSmallIcon(R.drawable.noti_icon) 
         .setContentTitle(title) 
         .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody)) 
         .setContentText(messageBody) 
         .setAutoCancel(true) 
         .setColor(this.getResources().getColor(R.color.colorAccent)) 
         .setSound(defaultSoundUri) 
         .setContentIntent(pendingIntent); 

       NotificationManager notificationManager = 
         (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

       notificationManager.notify(0, notificationBuilder.build()); 
      } 
     } 
public class FirebaseIDService extends FirebaseInstanceIdService { 
    private static final String TAG = "FirebaseIDService"; 

    @Override 
    public void onTokenRefresh() { 
     // Get updated InstanceID token. 
     String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
     Log.e(TAG, "Refreshed token: " + refreshedToken); 

     // TODO: Implement this method to send any registration to your app's servers. 
     sendRegistrationToServer(refreshedToken); 
    } 

    /** 
    * 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) { 
     // Add custom implementation, as needed. 
    } 
} 

而且在清單包含的類如:

<permission 
     android:name="com.pixelpoint.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <uses-permission android:name="com.pixelpoint.permission.C2D_MESSAGE" /> 

<service android:name=".MyFirebaseMessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
      </intent-filter> 
     </service> 
     <service android:name=".FirebaseIDService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
      </intent-filter> 
     </service> 

回答

4

你發送使用火力地堡控制檯的通知? 如果應用程序在後臺並且您的MyFirebaseMessagingService不會收到回叫,系統將處理這些通知。客戶端檢查用戶是否在本地設置中註冊以接收通知的代碼不適用於所有情況。 (更多關於此https://firebase.google.com/docs/cloud-messaging/android/receive後臺處理)

我對這個建議是建立一個主題,並自動註冊用戶後直接訂閱用戶到該主題:當用戶切換通知

FirebaseMessaging.getInstance().subscribeToTopic("news"); 

然後關閉,退訂主題。

FirebaseMessaging.getInstance().unsubscribeFromTopic("news"); 

這將從服務器上的列表中刪除它們,並且您不會依賴客戶端邏輯來過濾掉不需要的通知。

然後,當從Firebase控制檯向客戶端發送通知時,您應該僅針對那些註冊該主題的用戶

更多主題訊息在這裏 - https://firebase.google.com/docs/cloud-messaging/android/topic-messaging

+0

這是什麼(「新聞」),它是強制性的還是不是? – user6789978

+0

@ user6789978這是我正在使用的示例主題,您可以使用與您的應用程序相關的任何主題名稱。 – riggaroo