簡體   English   中英

僅當我禁用設置通知時才接收通知聲音

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

我有一個應用程序也可以從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.當您的應用程序處於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()方法中刪除以下handleDataMessage()行:

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

僅供參考,如果您要通過應用程序管理notification設置,請使用相應的preference key獲取notification settingsvalue並添加conditionplay通知聲音。

希望這會有所幫助〜

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM