0

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);
7
  • Read this parse.com/tutorials/android-push-notifications. You can have a custom class extending ParseBroadcastReceiver Commented Apr 8, 2015 at 11:59
  • pastebin.com/Djsk4HCk this is my code. Can you tell me where I'm wrong? When I click the notification I open the HomeActivity... the CustomParseBroadcastReceiver method onReceive is not called.. Commented Apr 8, 2015 at 12:27
  • this is not needed. PushService.setDefaultPushCallback(this, HomeActivity.class); Commented Apr 8, 2015 at 12:40
  • I've simplified my manifest code after I read more articles and now I have this. pastebin.com/xvQUTEaY If I remove the PushService.setDefaultPushCallback(this, HomeActivity.class); i won't get notifications anymore. And CustomParseBroadcastReceiver onReceive method is not called. Commented Apr 8, 2015 at 12:49
  • You need to show notification in broadcastreceiver. SOmehting like if(intent.hasExtra("com.parse.Data")) { try { json = new JSONObject(intent.getExtras().getString("com.parse.Data")); then show notification and on click you can navigate to a different activity Commented Apr 8, 2015 at 12:56

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.