Skip to content

Commit 9075b63

Browse files
Merge pull request #14397 from nextcloud/backport/12672/stable-3.30
[stable-3.30] Workaround (or fix maybe) for NEW_NOTIFICATION messages
2 parents da09608 + 8cd60b5 commit 9075b63

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

app/src/gplay/java/com/owncloud/android/services/firebase/NCFirebaseMessagingService.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
*/
88
package com.owncloud.android.services.firebase;
99

10+
import android.content.Intent;
1011
import android.text.TextUtils;
1112

13+
import com.google.firebase.messaging.Constants.MessageNotificationKeys;
1214
import com.google.firebase.messaging.FirebaseMessagingService;
1315
import com.google.firebase.messaging.RemoteMessage;
1416
import com.nextcloud.client.account.UserAccountManager;
1517
import com.nextcloud.client.jobs.BackgroundJobManager;
1618
import com.nextcloud.client.jobs.NotificationWork;
1719
import com.nextcloud.client.preferences.AppPreferences;
1820
import com.owncloud.android.R;
21+
import com.owncloud.android.lib.common.utils.Log_OC;
1922
import com.owncloud.android.utils.PushUtils;
2023

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

36+
static final String TAG = "NCFirebaseMessagingService";
37+
38+
// Firebase Messaging may apparently use two intent extras to specify a notification message.
39+
//
40+
// See the following fragments in https://github.com/firebase/firebase-android-sdk/blob/releases/m144_1.release/
41+
// firebase-messaging/src/main/java/com/google/firebase/messaging/FirebaseMessagingService.java#L223
42+
// firebase-messaging/src/main/java/com/google/firebase/messaging/NotificationParams.java#L419
43+
// firebase-messaging/src/main/java/com/google/firebase/messaging/Constants.java#L158
44+
//
45+
// The "old" key is not exposed in com.google.firebase.messaging.Constants.MessageNotificationKeys,
46+
// so we need to define it ourselves.
47+
static final String ENABLE_NOTIFICATION_OLD = MessageNotificationKeys.NOTIFICATION_PREFIX_OLD + "e";
48+
static final String ENABLE_NOTIFICATION_NEW = MessageNotificationKeys.ENABLE_NOTIFICATION;
49+
3350
@Override
3451
public void onCreate() {
3552
super.onCreate();
3653
AndroidInjection.inject(this);
3754
}
3855

56+
@Override
57+
public void handleIntent(Intent intent) {
58+
Log_OC.d(TAG, "handleIntent - extras: " +
59+
ENABLE_NOTIFICATION_NEW + ": " + intent.getExtras().getString(ENABLE_NOTIFICATION_NEW) + ", " +
60+
ENABLE_NOTIFICATION_OLD + ": " + intent.getExtras().getString(ENABLE_NOTIFICATION_OLD));
61+
62+
// When the app is in background and one of the ENABLE_NOTIFICATION or ENABLE_NOTIFICATION_OLD extras is set
63+
// to "1" in the intent sent from the FCM system code to the FirebaseMessagingService in the application,
64+
// the FCM library code that handles the intent DOES NOT invoke the onMessageReceived method.
65+
// It just displays the notification by itself.
66+
//
67+
// In our case the original FCM message contains dummy values "NEW_NOTIFICATION" and we need to get the
68+
// message in onMessageReceived to decrypt it.
69+
//
70+
// So we cheat here a little, by telling the FCM library that the notification flag is not set.
71+
//
72+
// Code below depends on implementation details of the firebase-messaging library (Firebase Android SDK).
73+
// https://github.com/firebase/firebase-android-sdk/tree/master/firebase-messaging
74+
75+
intent.removeExtra(ENABLE_NOTIFICATION_OLD);
76+
intent.removeExtra(ENABLE_NOTIFICATION_NEW);
77+
intent.putExtra(ENABLE_NOTIFICATION_NEW, "0");
78+
79+
super.handleIntent(intent);
80+
}
81+
3982
@Override
4083
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
84+
Log_OC.d(TAG, "onMessageReceived");
4185
final Map<String, String> data = remoteMessage.getData();
4286
final String subject = data.get(NotificationWork.KEY_NOTIFICATION_SUBJECT);
4387
final String signature = data.get(NotificationWork.KEY_NOTIFICATION_SIGNATURE);
@@ -48,6 +92,7 @@ public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
4892

4993
@Override
5094
public void onNewToken(@NonNull String newToken) {
95+
Log_OC.d(TAG, "onNewToken");
5196
super.onNewToken(newToken);
5297

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

0 commit comments

Comments
 (0)