Skip to content

Commit

Permalink
Merge pull request #12672 from nextcloud/fix/handle-new-notification
Browse files Browse the repository at this point in the history
Workaround (or fix maybe) for NEW_NOTIFICATION messages
  • Loading branch information
tobiasKaminsky authored Nov 11, 2024
2 parents bf8c6d8 + b7479c1 commit bcbefc0
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
*/
package com.owncloud.android.services.firebase;

import android.content.Intent;
import android.text.TextUtils;

import com.google.firebase.messaging.Constants.MessageNotificationKeys;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.jobs.BackgroundJobManager;
import com.nextcloud.client.jobs.NotificationWork;
import com.nextcloud.client.preferences.AppPreferences;
import com.owncloud.android.R;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.utils.PushUtils;

import java.util.Map;
Expand All @@ -30,14 +33,55 @@ public class NCFirebaseMessagingService extends FirebaseMessagingService {
@Inject UserAccountManager accountManager;
@Inject BackgroundJobManager backgroundJobManager;

static final String TAG = "NCFirebaseMessagingService";

// Firebase Messaging may apparently use two intent extras to specify a notification message.
//
// See the following fragments in https://github.com/firebase/firebase-android-sdk/blob/releases/m144_1.release/
// firebase-messaging/src/main/java/com/google/firebase/messaging/FirebaseMessagingService.java#L223
// firebase-messaging/src/main/java/com/google/firebase/messaging/NotificationParams.java#L419
// firebase-messaging/src/main/java/com/google/firebase/messaging/Constants.java#L158
//
// The "old" key is not exposed in com.google.firebase.messaging.Constants.MessageNotificationKeys,
// so we need to define it ourselves.
static final String ENABLE_NOTIFICATION_OLD = MessageNotificationKeys.NOTIFICATION_PREFIX_OLD + "e";
static final String ENABLE_NOTIFICATION_NEW = MessageNotificationKeys.ENABLE_NOTIFICATION;

@Override
public void onCreate() {
super.onCreate();
AndroidInjection.inject(this);
}

@Override
public void handleIntent(Intent intent) {
Log_OC.d(TAG, "handleIntent - extras: " +
ENABLE_NOTIFICATION_NEW + ": " + intent.getExtras().getString(ENABLE_NOTIFICATION_NEW) + ", " +
ENABLE_NOTIFICATION_OLD + ": " + intent.getExtras().getString(ENABLE_NOTIFICATION_OLD));

// When the app is in background and one of the ENABLE_NOTIFICATION or ENABLE_NOTIFICATION_OLD extras is set
// to "1" in the intent sent from the FCM system code to the FirebaseMessagingService in the application,
// the FCM library code that handles the intent DOES NOT invoke the onMessageReceived method.
// It just displays the notification by itself.
//
// In our case the original FCM message contains dummy values "NEW_NOTIFICATION" and we need to get the
// message in onMessageReceived to decrypt it.
//
// So we cheat here a little, by telling the FCM library that the notification flag is not set.
//
// Code below depends on implementation details of the firebase-messaging library (Firebase Android SDK).
// https://github.com/firebase/firebase-android-sdk/tree/master/firebase-messaging

intent.removeExtra(ENABLE_NOTIFICATION_OLD);
intent.removeExtra(ENABLE_NOTIFICATION_NEW);
intent.putExtra(ENABLE_NOTIFICATION_NEW, "0");

super.handleIntent(intent);
}

@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
Log_OC.d(TAG, "onMessageReceived");
final Map<String, String> data = remoteMessage.getData();
final String subject = data.get(NotificationWork.KEY_NOTIFICATION_SUBJECT);
final String signature = data.get(NotificationWork.KEY_NOTIFICATION_SIGNATURE);
Expand All @@ -48,6 +92,7 @@ public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {

@Override
public void onNewToken(@NonNull String newToken) {
Log_OC.d(TAG, "onNewToken");
super.onNewToken(newToken);

if (!TextUtils.isEmpty(getResources().getString(R.string.push_server_url))) {
Expand Down

0 comments on commit bcbefc0

Please sign in to comment.