在我的应用中关闭推送通知

时间:2016-07-25 00:13:27

标签: android firebase firebase-cloud-messaging

在我的应用中成功实施了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());

    }

}

想法好吗?提前谢谢!!

2 个答案:

答案 0 :(得分:1)

您可以添加某种共享Prefences,并存储从用户获得的值。然后在获得通知后首先获取值,如果用户的值在这种情况下为真,则跳过创建通知。

答案 1 :(得分: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变量为全局。