2016-07-25 79 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

相关问题