1
votes

I am trying to use parse for push notification in android, I am registering parse in the Application class like this:

@Override
public void onCreate() {
    super.onCreate();
    Parse.initialize(this, "***************", "***************");
}

I am subscribing to push like this in the main activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {

    ParsePush.subscribeInBackground("", new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if (e == null) {

                        ParseInstallation.getCurrentInstallation().saveInBackground(new SaveCallback() {
                            @Override
                            public void done(ParseException e) {


                                if (e == null) {
                                    String deviceToken = (String) ParseInstallation.getCurrentInstallation().get("deviceToken");
                                    Log.e("MainActivity", "device toekn " + deviceToken);

                                } else {
                                    Log.e("MainActivity", "failed to subscribe for push " + e.getLocalizedMessage());
                                }
                            }
                        });

            } else {
                Log.e("MainActivity", "failed to subscribe for push", e);
            }
        }
    });



    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

And here is my manifest section the apply to parse:

  <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="com.parse.ParsePushBroadcastReceiver"
        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's package name.
            -->
            <category android:name="com.****" />
        </intent-filter>
    </receiver>

however, device token returns null most of the times and the device has no device token in the parse dashboard, therefor, my device is not getting the push notifications.

The odd part is that sometimes it does work, i do get deviceToken and the installation object does get the push, has anyone encountered this behavior?

5

5 Answers

2
votes

This problem was eventually solved, a simple mistake in the Application name in the tag of the manifest in the tag. since the code i posted here was "com.***" no one could have helped, i apologize.

2
votes

I encountered this problem and i solved it by modifying my AndroidManifest file.

It appears that you can configure parse to either use "Parse Push Network" or GCM (Google Cloud Message).

Parse uses its own push network for apps that want to avoid a dependency on the Google Play Store, and for devices (like Kindles) which do not have Play Store support. Thus it is important to set the below in other to use GCM.

<uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.VIBRATE" />
 <uses-permission android:name="android.permission.WAKE_LOCK" />
 <uses-permission android:name="android.permission.GET_ACCOUNTS" />
 <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
 <permission android:name="YOUR_PACKAGE_NAME.permission.C2D_MESSAGE"
   android:protectionLevel="signature" />
 <uses-permission android:name="YOUR_PACKAGE_NAME.permission.C2D_MESSAGE" />

Replace YOUR_PACKAGE_NAME in the declarations above with your application's package name. Also, make sure that com.parse.GcmBroadcastReceiver and com.parse.PushService are declared as children of the element:

This worked for me, it might as well work for you.

Read this to get a full understanding

1
votes

I guess you are using version 1.9.3. In version 1.9.4 they have fixed the problem. Check the release notes.

1
votes

It could be useful to set parse logging to verbose to help debug further:

Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE);

With that I noticed that I was using the wrong "sender_id" in my manifest:

<meta-data android:name="com.parse.push.gcm_sender_id"
        android:value="id:123456789"/>

I'm using my own GCM which your not doing here...but LOG_LEVEL_VERBOSE might help others debug this.

-1
votes

Why are you saving the installation if you're not adding anything to it? ParseSDK does that for you already.

You must pass a valid "channel" parameter into subscribeInBackground: This is the channel that parse uses to push with; without it you won't get any pushes at all. https://parse.com/docs/android/api/com/parse/ParsePush.html#subscribeInBackground(java.lang.String,%20com.parse.SaveCallback)

In addition make sure you've added the needed tag in your manifest for parse push to work properly - and the needed just like it explained here:

https://parse.com/docs/android/guide#push-notifications-receiving-pushes