在我的应用中关闭推送通知
Posted
技术标签:
【中文标题】在我的应用中关闭推送通知【英文标题】:Turn off push notifications in my app 【发布时间】:2016-07-25 00:13:26 【问题描述】:在我的应用中成功实现了 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());
请出主意?提前谢谢!!
【问题讨论】:
如何设置一个首选项,如果需要,忽略推送。 关于添加共享首选项以跟踪用户是否希望收到通知的建议是公平的。但是请记住,这仅适用于 FCM 数据消息。如果您在应用处于后台时发送通知消息(例如来自 Firebase 控制台的消息),则会自动显示通知。 【参考方案1】:您可以添加某种共享首选项,并存储从用户那里获得的值。然后在收到通知后先获取值,如果用户的值在这种情况下为真,则跳过创建通知。
【讨论】:
下面给出了更好的视图! 虽然这听起来可行,但这不会不必要地消耗电池吗? 一点也不,虽然您也可以在节点中检索用户令牌,并且使用 Firebase 函数或您的服务器,您可以在该节点中搜索用户的令牌,如果存在,请不要发送推送通知,速度较慢,而且要花钱,这就是为什么共享偏好选项是最好的【参考方案2】:您可以为他们的偏好设置一个切换按钮,然后在执行代码之前检查切换的值。因此,它变得像 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
变量是全局变量。
【讨论】:
这在我的情况下不起作用,你能帮帮我吗以上是关于在我的应用中关闭推送通知的主要内容,如果未能解决你的问题,请参考以下文章