Skip to content

Commit 59e6104

Browse files
authored
Merge pull request #96 from hansemannn/shared
Android: use SharedPrefs for lastData
2 parents 7a26a75 + 8473084 commit 59e6104

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ Supported notification fields:
193193
* "sound" => "string" (e.g. "notification.mp3" will play /platform/android/res/raw/notification.mp3)
194194

195195
### Android: Note about custom sounds
196-
To use a custom sound > Android O you need to create a second channel. The default channel will always use the default notification sound on the device!
196+
To use a custom sound > Android O you need to create a second channel. The default channel will always use the default notification sound on the device!
197197

198198
### Android: Note for switching between v<=v2.0.2 and >=v2.0.3 if you use notification channels with custom sounds
199199
With versions prior to 2.0.3 of this module, FirebaseCloudMessaging.createNotificationChannel would create the notification sound uri using the resource id of the sound file in the `res/raw` directory. However, as described in this [android issue](https://issuetracker.google.com/issues/131303134), those resource ids can change to reference different files (or no file) between app versions, and that happens the notification channel may play a different or no sound than originally intended.
@@ -261,7 +261,7 @@ so receive the `gcm.message_id` key from the notification payload instead.
261261
`apnsToken` (String, set) (iOS only)
262262

263263
`lastData` (Object) (Android only)
264-
The propery `lastData` will contain the data part when you send a notification push message (so both nodes are visible inside the push payload).
264+
The propery `lastData` will contain the data part when you send a notification push message (so both nodes are visible inside the push payload). Read before calling `registerForPushNotifications()`.
265265

266266
#### Events
267267

@@ -331,10 +331,16 @@ if (OS_ANDROID) {
331331
showBadge: true
332332
});
333333
// if you use a custom id you have to set the same to the `channelId` in you php send script!
334-
334+
335335
fcm.notificationChannel = channel;
336336
}
337337

338+
339+
if (OS_ANDROID){
340+
// display last data:
341+
Ti.API.info("Last data: " + fcm.lastData);
342+
}
343+
338344
// Register the device with the FCM service.
339345
fcm.registerForPushNotifications();
340346

@@ -347,11 +353,6 @@ if (fcm.fcmToken) {
347353

348354
// Subscribe to a topic.
349355
fcm.subscribeToTopic('testTopic');
350-
351-
if (OS_ANDROID){
352-
// display last data:
353-
Ti.API.info("Last data: " + fcm.lastData);
354-
}
355356
```
356357

357358
## Send FCM messages with PHP

android/manifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# this is your module manifest and used by Titanium
33
# during compilation, packaging, distribution, etc.
44
#
5-
version: 2.0.3
5+
version: 2.0.4
66
apiversion: 4
77
architectures: arm64-v8a armeabi-v7a x86
88
description: titanium-firebase-cloud-messaging

android/src/firebase/cloudmessaging/CloudMessagingModule.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ private KrollDict getLastData()
8383
data.put("inBackground", true);
8484
}
8585
}
86+
87+
if (data.get("message") == null) {
88+
SharedPreferences preferences =
89+
PreferenceManager.getDefaultSharedPreferences(Utils.getApplicationContext());
90+
String prefMessage = preferences.getString("titanium.firebase.cloudmessaging.message", null);
91+
if (prefMessage != null) {
92+
data.put("message", new KrollDict(new JSONObject(prefMessage)));
93+
}
94+
preferences.edit().clear().commit();
95+
}
8696
} catch (Exception ex) {
8797
Log.e(LCAT, "getLastData" + ex);
8898
}
@@ -334,5 +344,8 @@ public void parseBootIntent()
334344
} catch (Exception ex) {
335345
Log.e(LCAT, "parseBootIntent" + ex);
336346
}
347+
348+
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Utils.getApplicationContext());
349+
preferences.edit().clear().commit();
337350
}
338351
}

android/src/firebase/cloudmessaging/TiFirebaseMessagingService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import android.app.PendingIntent;
66
import android.content.Context;
77
import android.content.Intent;
8+
import android.content.SharedPreferences;
89
import android.graphics.Bitmap;
910
import android.graphics.BitmapFactory;
1011
import android.media.RingtoneManager;
1112
import android.net.Uri;
1213
import android.os.Build;
14+
import android.preference.PreferenceManager;
1315
import android.support.v4.app.NotificationCompat;
1416
import android.util.Log;
1517
import com.google.firebase.messaging.FirebaseMessagingService;
@@ -150,6 +152,11 @@ private Boolean showNotification(RemoteMessage remoteMessage)
150152
builder_defaults |= Notification.DEFAULT_SOUND;
151153
}
152154

155+
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
156+
SharedPreferences.Editor editor = preferences.edit();
157+
editor.putString("titanium.firebase.cloudmessaging.message", jsonData.toString());
158+
editor.commit();
159+
153160
if (!showNotification) {
154161
// hidden notification - still send broadcast with data for next app start
155162
Intent i = new Intent();

0 commit comments

Comments
 (0)