Unable to read Parse push notification package data

I am trying to send user data using the push search notification service, but when fetching from the Bundle, null values ​​are always returned.

User Broadcast Receiver:

@Override public void onReceive(Context context, Intent intent) { Log.e("Receiver","Invoked"); Bundle bundle = intent.getExtras(); Intent contentIntent = new Intent(context, MainPage.class); String alertMsg = bundle.getString("heading"); //never get this value String urlString = bundle.getString("dataString"); //never get this value contentIntent.putExtra(EXTRA_URL, urlString); PendingIntent pendingIntent = PendingIntent.getActivity(context, NOTIFY_REQUEST_CODE, contentIntent, PendingIntent.FLAG_ONE_SHOT); showNotification(context, notificationId, alertMsg, pendingIntent); } 

Manifest declaration:

  <receiver android:name=".receiver.NotificationReceiver" android:exported="false"> <intent-filter> <action android:name="link_notification"/> </intent-filter> </receiver> 

And I send the following JSON from the syntax analysis panel:

 { "dataString": "objectId", "heading": "type", "action": "link_notification" } 

When I log Bundle data, I can see the header and dataString , but can't access it. A package always returns null.

Please, help! Thanks.

+5
source share
3 answers

JSON will be in an additional line com.parse.Data .

 @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); String jsonData = extras.getString("com.parse.Data"); JSONObject jsonObject; try { jsonObject = new JSONObject(jsonData); String heading = jsonObject.getString("heading"); String dataString = jsonObject.getString("dataString"); } catch (JSONException e) { e.printStackTrace(); } } 
+16
source

OK, now I get it. To get the Json String, you need to do the following:

 public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Bundle extra = intent.getExtras(); String json = extra.getString("com.parse.Data"); } 
+3
source

This is because the package data contains the json object as a string that can be recovered using a key that you can verify when parsing. Only then do you need to parse this string with gson, for example, to get Java objects.

0
source

Source: https://habr.com/ru/post/1204860/


All Articles