forked from invertase/react-native-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[messaging] Support FCM data-only messages in the background
- Loading branch information
1 parent
57ffa9b
commit 7ce7f5a
Showing
7 changed files
with
122 additions
and
48 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
android/src/main/java/io/invertase/firebase/messaging/MessagingSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.invertase.firebase.messaging; | ||
|
||
import com.facebook.react.bridge.Arguments; | ||
import com.facebook.react.bridge.WritableMap; | ||
import com.google.firebase.messaging.RemoteMessage; | ||
|
||
import java.util.Map; | ||
|
||
|
||
public class MessagingSerializer { | ||
public static WritableMap parseRemoteMessage(RemoteMessage message) { | ||
WritableMap messageMap = Arguments.createMap(); | ||
WritableMap dataMap = Arguments.createMap(); | ||
|
||
if (message.getCollapseKey() != null) { | ||
messageMap.putString("collapseKey", message.getCollapseKey()); | ||
} | ||
|
||
if (message.getData() != null) { | ||
for (Map.Entry<String, String> e : message.getData().entrySet()) { | ||
dataMap.putString(e.getKey(), e.getValue()); | ||
} | ||
} | ||
messageMap.putMap("data", dataMap); | ||
|
||
if (message.getFrom() != null) { | ||
messageMap.putString("from", message.getFrom()); | ||
} | ||
if (message.getMessageId() != null) { | ||
messageMap.putString("messageId", message.getMessageId()); | ||
} | ||
if (message.getMessageType() != null) { | ||
messageMap.putString("messageType", message.getMessageType()); | ||
} | ||
messageMap.putDouble("sentTime", message.getSentTime()); | ||
if (message.getTo() != null) { | ||
messageMap.putString("to", message.getTo()); | ||
} | ||
messageMap.putDouble("ttl", message.getTtl()); | ||
|
||
return messageMap; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...d/src/main/java/io/invertase/firebase/messaging/RNFirebaseBackgroundMessagingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.invertase.firebase.messaging; | ||
|
||
import android.content.Intent; | ||
import android.os.Bundle; | ||
|
||
import com.facebook.react.HeadlessJsTaskService; | ||
import com.facebook.react.bridge.Arguments; | ||
import com.facebook.react.bridge.WritableMap; | ||
import com.facebook.react.jstasks.HeadlessJsTaskConfig; | ||
import com.google.firebase.messaging.RemoteMessage; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
public class RNFirebaseBackgroundMessagingService extends HeadlessJsTaskService { | ||
@Override | ||
protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) { | ||
Bundle extras = intent.getExtras(); | ||
if (extras != null) { | ||
RemoteMessage message = intent.getParcelableExtra("message"); | ||
WritableMap messageMap = MessagingSerializer.parseRemoteMessage(message); | ||
return new HeadlessJsTaskConfig( | ||
"RNFirebaseBackgroundMessage", | ||
messageMap, | ||
10000, | ||
false | ||
); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
import { AppRegistry } from 'react-native'; | ||
import bootstrap from './src/main'; | ||
import bgMessaging from './src/bgMessaging'; | ||
|
||
AppRegistry.registerComponent('ReactNativeFirebaseDemo', () => bootstrap); | ||
// Task registered to handle background data-only FCM messages | ||
AppRegistry.registerHeadlessTask( | ||
'RNFirebaseBackgroundMessage', | ||
() => bgMessaging | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import RNfirebase from './../firebase'; | ||
|
||
export default async message => { | ||
console.log('Message', message); | ||
|
||
const notification = new RNfirebase.notifications.Notification(); | ||
notification | ||
.setTitle('Background notification') | ||
.setBody('Background body') | ||
.setNotificationId('background') | ||
.android.setChannelId('test') | ||
.android.setClickAction('action') | ||
.android.setPriority(RNfirebase.notifications.Android.Priority.Max); | ||
|
||
await RNfirebase.notifications().displayNotification(notification); | ||
return Promise.resolve(); | ||
}; |