Boldijar Paul
Boldijar Paul

Reputation: 5495

Parse.com android notification - get data into BroadcastReceiver

I need help implementing a ParsePushBroadcastReceiver, in order to use it to parse a JSON Object, to choose an Activity to start.

I've tried with:

 <receiver
            android:name="com......BroadcastReceiver"
            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>

Or

<receiver android:name="com....BroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>

But no success...

My application class

public class AppApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        System.out.println("vrum");
        Parse.initialize(this, "...", "..");
        PushService.setDefaultPushCallback(this, HomeActivity.class);
        ParseInstallation.getCurrentInstallation().saveInBackground();

    }

When I click the notification I get to the HomeActivity...same thing if I remove that line

 PushService.setDefaultPushCallback(this, HomeActivity.class);

Upvotes: 0

Views: 528

Answers (1)

droidx
droidx

Reputation: 2172

 public class ParseCustomBroadcastReceiver extends ParsePushBroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            try {

    // Sample code
                JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
                final String notificationTitle = json.getString("title").toString();
                final String notificationContent = json.getString("alert").toString();
                final String uri = json.getString("uri");

    //Create a taskstack builder - this is just sample snippet to give an idea
                Intent resultIntent = null;
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
resultIntent = new Intent(context, NotificationOpenActivity.class);
stackBuilder.addParentStack(NotificationOpenActivity.class);
stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);


                // Customize your notification
                NotificationCompat.Builder builder =
                        new NotificationCompat.Builder(context)
                                .setSmallIcon(R.mipmap.ic_notification_icon)
                                .setContentTitle(notificationTitle)
                                .setContentText(notificationContent)
                                .setGroup(GROUP_SHORTR_NOTIFS)
                                .setContentIntent(resultPendingIntent)
                                .setAutoCancel(true)
                                .setVisibility(Notification.VISIBILITY_PUBLIC)
                                .setDefaults(Notification.DEFAULT_VIBRATE)
                                .setStyle(new NotificationCompat.BigTextStyle()
                                        .bigText(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());
            }

        }
    }

Add the following in the manifest.

<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>

Basically, for the manifest edits picking up from your question, simply edit the android:name property.

Hope this helps!

Upvotes: 1

Related Questions