Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Implementing Designs for Android Adding Push Notifications from Parse.com Setting Up Push Notifications

Daniel Hartin
Daniel Hartin
18,106 Points

PushService.setDefaultPushCallback() has now been depreciated. What can be used?

Hi all the above Class/method is now marked as depreciated, it still works but just wondered if anyone knew the Class/method that has replaced this one.

I have looked in the documentation and I can't find anything relating to this code at all, although without it, the app doesn't receive the push notification.

Any ideas?

I second that... I found this: Intent intent = new Intent(this, MainActivity.class); new ParsePushBroadcastReceiver().onReceive(this, intent); but it doesn't work...

Daniel Hartin
Daniel Hartin
18,106 Points

Yep! tried the same thing with the same result, following some examples I found in an obscure forum somewhere. It's a shame really, it's the only bum code I've found on this track that I haven't found the answer for relatively easy. Parse documentation is sketchy as well.

Hopefully Ben will see this at some point and put us right!

6 Answers

Daniel Schmidt
Daniel Schmidt
1,683 Points

See my answer in this thread. It addresses the changes you need to make for Parse 1.7.

For the newer versions of parse you no longer need PushService.setDefaultPushCallback(). You only need the second line for push notifications to work properly:

ParseInstallation.getCurrentInstallation().saveInBackground();

Testing Parse v1.7.1 and v1.8.0, I did NOT need to use PushService.setDefaultPushCallback(this, MainActivity.class) to receive the notification. I just needed ParseInstallation.getCurrentInstallation().saveInBackground(), even though the current quickstart code removed that line. With all of the default Parse quickstart code in place (just the package name changed), I was able to receive the test push notifications.

In v1.7.1 and v1.8.0, it seems that PushService.setDefaultPushCallback(this, MainActivity.class) didn't work at all because with or without that line when I clicked the notification, I got the crash error:

java.lang.RuntimeException: Unable to start receiver com.parse.ParsePushBroadcastReceiver: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=null flg=0x1000c000 (has extras) }
...
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat= flg=0x1000c000 (has extras) }

So, looking into the [v1.8.0] javadoc that came with the SDK zip, I found that for com.parse.PushService.setDefaultPushCallBack(), it says "Deprecated. Replaced by ParsePushBroadcastReceiver". Then, when I look at the javadoc for com.parse.ParsePushBroadcastReceiver, it says "... ParsePushBroadcastReceiver can be subclassed. ...<other ways to customize it> ... To change the Activity launched when a user opens a Notification, override ParsePushBroadcastReceiver.getActivity(Context, Intent)."

That's the only Parse recommended way to change the default Activity that I have found so far. But, digging deeper...

The source code for that method that they want you to override is:

protected Class<? extends Activity> getActivity(Context context, Intent intent) {
    String packageName = Parse.applicationContext.getPackageName();
    Intent launchIntent = Parse.applicationContext.getPackageManager().getLaunchIntentForPackage(packageName);
    if (launchIntent == null) {
        return null;
    }
    String className = launchIntent.getComponent().getClassName();
    Class<? extends Activity> cls = null;
    try {
        cls = Class.forName(className);
    } catch (ClassNotFoundException e) {}
    return cls;
}

At a glance, it looks like by default it should be working for us, especially since this Treehouse project's default launch activity is MainActivity. The documentation for [PackageManager.getLaunchForPackage(String)](http://developer.android.com/reference/android/content/pm/PackageManager.html#getLaunchIntentForPackage(java.lang.String) shows it looks for an Activity with CATEGORY_INFO, then CATEGORY_LAUNCHER in the AndroidManifest. Unfortunately, neither of those worked.

When I get time later, I might implement it and add some debug lines to see what's exactly happening, or unless somebody else posts about it first. ;)

ps - For these tests I was using a HTC Evo running API 16 and a Nexus 4 running API 21.

To better answer the question in a more readable manner... I was going to explain the root problem myself, but instead found a good StackOverflow question that has two working answers.

Currently, the first answer has 53 upvotes and has shorter code, the second answer has 16 upvotes and longer code.

The first answer is a workaround that basically works, but limits some things. The second one basically fixes the Parse API bug and leaves the other Parse feature intact that's mentioned here in Customizing your Notifications. And, with the second way, you still get to override getActivity() that I mentioned in my other post on this thread.

Bruce Parmele
Bruce Parmele
6,474 Points

Does the Treehouse staff have an official remedy for this issue?

This is from Parse.com. I used it and it works. Add the following to your RibbitApplication instead of what is shown on the video for PushService.......

ParsePush.subscribeInBackground("", new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
                } else {
                    Log.e("com.parse.push", "failed to subscribe for push", e);
                }
            }
        });

sheesh, sorry about the formatting

Daniel Hartin
Daniel Hartin
18,106 Points

No problem, I edited the formatting for you.

This is not working for me...did you have to do anything else?