Parse SDK implements custom BroadcastReceiver for push notifications in android

I am trying to start with a Parse browser alert on Android, but I could not find a way to implement my own BroadcastReceiver that will take action in response to a push notification.

In their "beginning" they give an example of a program in which the receiver and the intent service are contained in a jar file (and are not implemented in the code so that I can try and play with it). A similar question was asked here , but he is two years old and the answer has 11 downvotes.

There are several other blog posts, for example. as in here , but none of them work properly (the one that had two years in this link).

Therefore, please suggest a phased way to implement the same.

EDIT:

I tried the code suggested by droidx. But that does not work. I submit my AndroidManifest.xml (the rest of the code is exactly the same as it suggested):

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.rahulserver.parsenotificationdemo" > <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <!-- IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below to match your app package name + ".permission.C2D_MESSAGE". --> <permission android:protectionLevel="signature" android:name="com.rahulserver.parsenotificationdemo.permission.C2D_MESSAGE" /> <uses-permission android:name="com.rahulserver.parsenotificationdemo.permission.C2D_MESSAGE" /> <application android:name=".ParseNotification" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.parse.PushService" /> <receiver android:name="com.parse.ParseBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.USER_PRESENT" /> </intent-filter> </receiver> <receiver android:name=".MyCustomParsePushReceiver" android:exported="false"> <intent-filter> <action android:name="com.parse.push.intent.RECEIVE" /> <action android:name="com.parse.push.intent.DELETE" /> <action android:name="com.parse.push.intent.OPEN" /> </intent-filter> </receiver> <receiver android:name="com.parse.GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <!-- IMPORTANT: Change "com.parse.starter" to match your app package name. --> <category android:name="com.rahulserver.parsenotificationdemo" /> </intent-filter> </receiver> </application> </manifest> 
0
source share
1 answer

Here is an example!

 public class ParseCustomBroadcastReceiver extends ParsePushBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { try { JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data")); Log.d(TAG, json.getString("alert").toString()); final String notificationTitle = json.getString("title").toString(); final String notificationContent = json.getString("alert").toString(); final String uri = json.getString("uri"); Intent resultIntent = null; TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); resultIntent = new Intent(context, HomeScreen.class); stackBuilder.addParentStack(HomeScreen.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); //Customize your notification - sample code NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(R.mipmap.ic_notification_icon) .setContentTitle(notificationTitle) .setContentText(notificationContent); int mNotificationId = 001; NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); mNotifyMgr.notify(mNotificationId, builder.build()); } catch (JSONException e) { Log.d(TAG, e.getMessage()); } } } 

Following this guide, replace the android name for the recipient with the path of your custom translator

 <receiver android:name=".receivers.ParseCustomBroadcastReceiver" android:exported="false" > <intent-filter> <action android:name="com.parse.push.intent.RECEIVE" /> <action android:name="com.parse.push.intent.DELETE" /> <action android:name="com.parse.push.intent.OPEN" /> </intent-filter> </receiver> 

In your application class, be sure to register for Parse Push, as shown below.

 Parse.initialize(this, ParseUtils.PARSE_APPLICATION_ID_DEV, ParseUtils.PARSE_CLIENT_KEY_DEV); ParseObject.registerSubclass(Article.class); ParsePush.subscribeInBackground("", new SaveCallback() { @Override public void done(ParseException e) { if (e == null) { Log.d(TAG, "successfully subscribed to the broadcast channel."); } else { Log.e(TAG, "failed to subscribe for push", e); } } }); 

Update:

The aforementioned broadcast receiver expects to send a notification about a json object from the parsing pane, usually consisting of three pairs of name values:

 { "alert": "Alert message", "title": "Title", "uri": "bla bla" } 

When you use only a regular string, for example: hello world! in the syntax panel, click the panel, internally it will become only a warning tag. something like this: {"alert" : "hello world!, "title" = null/"", "uri" = null/""}

The broadcast receiver code above does not have these null checks, so it gets caught with the exception No Title / uri.

So, you can add some null checks based on what is expected from the push notification.

Something like that:

 JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data")); Log.d(TAG, json.getString("alert").toString()); if (json.has("title")) { notificationTitle = json.getString("title").toString(); } if (json.has("alert")) { notificationAlert = json.getString("alert").toString(); } if(json.has("uri")) { notificationURI = json.getString("uri"); } 

Then set the appropriate contentTitle and contentText in the notification

  NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(notificationAlert) .setContentText(notificationAlert); 

Hope this helps!

notification example - plain string

+6
source

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


All Articles