简体   繁体   English

仅当我禁用设置通知时才接收通知声音

[英]Receiving notification sound only when I disable notification from setting

I have an application that receive notification from firebase console also from php, but I have a problem, that when I disabled notification from settings , then I still receive only sound of notification. 我有一个应用程序也可以从php从firebase控制台接收通知,但是我有一个问题,当我从settings禁用通知时,我仍然只收到通知声音。 I found the code that causes this: I add a two lines of code to receive the notification when the app is open(inforeground) it works and I received notification when app is in foreground and I need it in my app. 我找到了引起这种情况的代码:我添加了两行代码以在应用程序打开(在前台)时接收通知,并且可以正常工作;当应用程序在前台运行且我需要在应用程序中接收通知。 But it causes the problem I mentioned above, so can any one help me to make app receive notification when its open and without causing this problem? 但这会导致上述问题,因此有人可以帮助我让应用在打开时收到通知,而不会引起此问题吗?

Here's the code I add in the MyFirebaseMessagingService: 这是我添加到MyFirebaseMessagingService中的代码:

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

And here is the complete code of MyFirebaseMessagingService (by the way I am sending the notification from php) 这是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. When your app is in foreground state, below lines of codes will be executed: 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());
}

To disable notification sound, update handleNotification() method as below: 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. When your app is in background state, below lines of codes will be executed: 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());
    }
}

To disable notification sound, remove below lines from handleDataMessage() method: disable通知声音,请从handleDataMessage()方法中删除以下handleDataMessage()行:

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

FYI, If you are managing notification settings from your app, then use the respective preference key to get the value of notification settings and add condition to play notification sound. 仅供参考,如果您要通过应用程序管理notification设置,请使用相应的preference key获取notification settingsvalue并添加conditionplay通知声音。

Hope this will help~ 希望这会有所帮助〜

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM