仅当我从设置中禁用通知时才收到通知声音

Posted

技术标签:

【中文标题】仅当我从设置中禁用通知时才收到通知声音【英文标题】:Receiving notification sound only when I disable notification from setting 【发布时间】:2017-05-12 06:28:30 【问题描述】:

我有一个应用程序也从 php 接收来自 firebase 控制台的通知,但我有一个问题,当我禁用来自 settings 的通知时,我仍然只收到通知的声音。我找到了导致这种情况的代码:我添加了两行代码以在应用程序打开时接收通知(在前台)它可以工作,并且当应用程序处于前台并且我在我的应用程序中需要它时我收到通知。但这会导致我上面提到的问题,那么任何人都可以帮助我让应用程序在打开时收到通知并且不会导致这个问题吗?

这是我在 MyFirebaseMessagingService 中添加的代码:

Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);

这是 MyFirebaseMessagingService 的完整代码(顺便说一下,我是从 php 发送通知)

public class MyFirebaseMessagingService extends FirebaseMessagingService 
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();

private NotificationUtils notificationUtils;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) 
    Log.e(TAG, "From: " + remoteMessage.getFrom());





    if (remoteMessage == null)
        return;

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) 
        Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
        handleNotification(remoteMessage.getNotification().getBody());
    

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) 
        Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

        try 
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            handleDataMessage(json);
         catch (Exception e) 
            Log.e(TAG, "Exception: " + e.getMessage());
        
    


private void handleNotification(String message) 
    if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) 
        // app is in foreground, broadcast the push message
        Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
        pushNotification.putExtra("message", message);
        LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

        //added code(1) by me to try receiving the notification when App in open:




        //end Code(1)
        // play notification sound
        NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
        notificationUtils.playNotificationSound();
    else
        // If the app is in background, firebase itself handles the notification
    


private void handleDataMessage(JSONObject json) 
    Log.e(TAG, "push json: " + json.toString());

    try 
        JSONObject data = json.getJSONObject("data");

        String title = data.getString("title");
        String message = data.getString("message");
        boolean isBackground = data.getBoolean("is_background");
        String imageUrl = data.getString("image");
        String timestamp = data.getString("timestamp");
        JSONObject payload = data.getJSONObject("payload");

        Log.e(TAG, "title: " + title);
        Log.e(TAG, "message: " + message);
        Log.e(TAG, "isBackground: " + isBackground);
        Log.e(TAG, "payload: " + payload.toString());
        Log.e(TAG, "imageUrl: " + imageUrl);
        Log.e(TAG, "timestamp: " + timestamp);




            if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) 
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

            // added code(2) trying receive notification when app is open
            Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
            showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);





            //end (code(2)


            // play notification sound
            //NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
           // notificationUtils.playNotificationSound();
         else 
            // app is in background, show the notification in notification tray
            Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
            resultIntent.putExtra("message", message);

            // check for image attachment
            if (TextUtils.isEmpty(imageUrl)) 
                showNotificationMessage(getApplicationContext(), title, message, timestamp, resultIntent);
             else 
                // image is present, show notification with image
                showNotificationMessageWithBigImage(getApplicationContext(), title, message, timestamp, resultIntent, imageUrl);
            
        
     catch (JSONException e) 
        Log.e(TAG, "Json Exception: " + e.getMessage());
     catch (Exception e) 
        Log.e(TAG, "Exception: " + e.getMessage());
    



 // Showing notification with text only

private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) 
    notificationUtils = new NotificationUtils(context);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    notificationUtils.showNotificationMessage(title, message, timeStamp, intent);



// Showing notification with text and image

private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) 
    notificationUtils = new NotificationUtils(context);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);

【问题讨论】:

【参考方案1】:

1.当您的应用处于foreground 状态时,将执行以下代码行:

// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) 
    Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
    handleNotification(remoteMessage.getNotification().getBody());

disable通知声音,更新handleNotification()方法如下:

private void handleNotification(String message) 
    if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) 
        // app is in foreground, broadcast the push message
        Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
        pushNotification.putExtra("message", message);
        LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

    

2.当您的应用处于background 状态时,将执行以下代码行:

// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) 
    Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

    try 
        JSONObject json = new JSONObject(remoteMessage.getData().toString());
        handleDataMessage(json);
     catch (Exception e) 
        Log.e(TAG, "Exception: " + e.getMessage());
    

对于disable 通知声音,从handleDataMessage() 方法中删除以下行:

// play notification sound
NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.playNotificationSound();

仅供参考,如果您正在从您的应用管理notification 设置,则使用相应的preference key 获取notification settingsvalue 并将condition 添加到play通知声音。

希望对你有帮助~

【讨论】:

我更新了你提到的 handleNotification() 以及句柄 handleDataMessage() 并且当从手机设置中禁用通知时我仍然听到通知声音(当应用程序打开时我都听到了声音并关闭)!! 我正在禁用来自手机设置的通知,而不是来自我的应用程序

以上是关于仅当我从设置中禁用通知时才收到通知声音的主要内容,如果未能解决你的问题,请参考以下文章

无法为奥利奥通知启用闪烁灯并禁用通知声音

如何在我的应用程序中获取推送通知设置?

Android:通知动作点击非统一行为

是否可以在 iOS 7 上从我的应用程序中禁用推送通知声音?

在 Android 的正在进行的通知更新中禁用声音

在 iOS 设置中更改通知时,Amazon SNS 如何禁用/重新启用设备?