For AI agents: see the complete documentation index at /llms.txt.
Parse push fields
Updated
Parse push notifications fields.
After receiving a push notification, you need to parse the data.
Rewrite the FirebaseMessagingService.onMessageReceived method to get the custom extension field in the RemoteMessage object. The sample code is as follows:
public class FCMMSGService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().size() > 0) {
String f = remoteMessage.getData().get("f");
String t = remoteMessage.getData().get("t");
String m = remoteMessage.getData().get("m");
String g = remoteMessage.getData().get("g");
Object e = remoteMessage.getData().get("e");
}
}
@Override
public void handleIntent ( @NonNull Intent intent ) {
super.handleIntent(intent);
Bundle bundle = intent . getExtras();
if (bundle != null) {
Map map = new HashMap<>();
for (String key : bundle.keySet()) {
if (!TextUtils.isEmpty(key)) {
Object content = bundle.get(key);
map.put(key, content);
}
}
Log.i(TAG, "handleIntent: " + map);
}
}
}| Parameter | Description |
|---|---|
f | The user ID of the push notification sender. |
t | The user ID of the push notification recipient. |
m | The message ID: A unique identifier of the message. |
g | The group ID: This field exists only for group messages. |
e | The user-defined extension field. |
e is a completely user-defined extension. The data source is em_push_ext.custom of the message extension. The data structure is as follows:
{
"em_push_ext": {
"custom": {
"key1": "value1",
"key2": "value2"
}
}
}The data structure of the extension in the RemoteMessage object is as follows:
{
"t":"receiver",
"f":"fromUsername",
"m":"msg_id",
"g":"group_id",
"e":{}
}When the device receives a push notification and a user clicks it, the system will pass the custom push content (JSON) to the app. This allows you to customize the behavior triggered by clicking the push notification according to the push content, such as page routing. When a push notification is received and clicked, the app obtains the push content as follows:
-
If
SceneDelegateis used in the app, the app startup process is managed by the scene system. When you click the offline push message to open the app, the app will start the scene and then call the corresponding method inSceneDelegateto handle the connection and configuration of the scene. Check theconnectionOptionsparameter in thescene ( _ : willConnectTo:options:)method ofSceneDelegateto get the push content. The sample code is as follows:- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions { // Get the startup options NSDictionary *launchOptions = connectionOptions.notificationResponse.notification.request.content.userInfo; // Perform corresponding processing // ... } -
If
SceneDelegateis not used in the app, the system will pass the user-defined information in the push to the app throughlaunchOptionsin theapplication:didFinishLaunchingWithOptions:method. Check thelaunchOptionsparameter to get the push content.- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; }
The data structure of the user-defined userInfo is as follows:
{
"aps":{
"alert":{
"body" : "You have a new message"
},
"badge":1,
"sound":"default"
},
"f":"6001",
"t":"6006",
"g":"1421300621769",
"m":"373360335316321408"
}| Parameter | Description |
|---|---|
body | The push content. |
badge | The badge. |
sound | The ringtone. |
f | The message sender ID. |
t | The message recipient ID. |
g | The group ID. This field exists only for group messages. |
m | The message ID. |
After receiving a push notification, you need to parse the data.
Use the FirebaseMessaging.instance.getInitialMessage method to obtain the custom extension fields when the app is opened. The sample code is as follows:
FirebaseMessaging.instance.getInitialMessage().then((message) {
Map? data = message?.data;
if (data != null) {
String f = data['f'] ?? '';
String t = data['t'] ?? '';
String m = data['m'] ?? '';
String g = data['g'] ?? '';
Object e = data['e'] ?? '';
}
});| Parameter | Description |
|---|---|
f | The user ID of the push notification sender. |
t | The user ID of the push notification recipient. |
m | The message ID: A unique identifier of the message. |
g | The group ID: This field exists only for group messages. |
e | The user-defined extension field. |
e is a completely user-defined extension. The data source is em_push_ext.custom of the message extension. The data structure is as follows:
{
"em_push_ext": {
"custom": {
"key1": "value1",
"key2": "value2"
}
}
}The data structure of the extension in the RemoteMessage.data object is as follows:
{
"t":"receiver",
"f":"fromUsername",
"m":"msg_id",
"g":"group_id",
"e":{}
}After receiving a push notification, you need to parse the data.
Rewrite the FirebaseMessagingService.onMessageReceived method to get a custom extension field in the RemoteMessage object. The sample code is as follows:
messaging().onMessage(async (remoteMessage) => {
console.log("A new FCM message arrived!", JSON.stringify(remoteMessage));
// "t":"receiver",
// "f":"fromUsername",
// "m":"msg_id",
// "g":"group_id",
// "e":{}
});| Parameter | Description |
|---|---|
f | The user ID of the push notification sender. |
t | The user ID of the push notification recipient. |
m | The message ID: A unique identifier of the message. |
g | The group ID: This field exists only for group messages. |
e | The user-defined extension field. |
e is a completely user-defined extension. The data source is em_push_ext.custom of the message extension. The data structure is as follows:
{
"em_push_ext": {
"custom": {
"key1": "value1",
"key2": "value2"
}
}
}The data structure of the extension in the RemoteMessage object is as follows:
{
"t": "receiver",
"f": "fromUsername",
"m": "msg_id",
"g": "group_id",
"e": {}
}This feature guide is not available yet.
This feature is not supported for this platform.
This feature is not supported for this platform.
