在我的应用程序中关闭推送通知
问题描述:
在我的应用程序中成功实施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());
}
}
想法请好吗?提前致谢!!
答
您可以为自己的偏好设置一个切换按钮,然后在执行代码之前检查切换的值。因此,它变得像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
如何设置一个pref,并忽略推动如果想要的。 – lionscribe
关于添加一个跟踪用户是否想要通知的共享pref的建议是公平的。但请记住,这仅适用于FCM数据消息。如果您在应用处于后台时发送通知消息(如来自Firebase控制台的通知消息),则会自动显示通知。 –