Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse notifications on android won't work

I am trying to set up Parse push notifications on android for some time now. I followed different tutorials, and I am trying to send the notifications from their web platform, but nothing seems to work. Here is what I have tried so far.

Here is the onCreate method of my Application class

@Override
    public void onCreate() {
        super.onCreate();
        Log.i("PushNotifications","PARSE initialize");

        Parse.initialize(this, "******************************", "*****************************");
        ParseInstallation.getCurrentInstallation().saveInBackground();

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

    }

This is called successfully, as I get the log

successfully subscribed to the broadcast channel.

Also here is some relevant content of my manifest file:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<permission android:name="com.my.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.my.app.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:name=".App"
    android:theme="@style/AppTheme">

    <activity
        android:name=".activities.MainActivity"
        android:launchMode="standard"
        android:screenOrientation="sensorPortrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data android:name="com.parse.push.gcm_sender_id" android:value="id:123456789" />
    <meta-data android:name="com.parse.push.notification_icon" android:resource="@drawable/ic_launcher"/>

    <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>
    <!--com.parse.GcmBroadcastReceiver"-->
    <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" />
            <category android:name="com.my.app" />
        </intent-filter>
    </receiver>

</application>

When I try sending push from Parse website, nothing happens.

As this is not working, I tried implementing my own GcmBroadcastReceiver and changing it in the manifest.

<receiver android:name=".receivers.MyCustomReceiver"
            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" />
                <category android:name="com.my.app" />
            </intent-filter>
</receiver>

And

public class MyCustomReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("PushNotifications","MyCustomReceiver");
    }
}

Without success.

Then I also tried creating my own ParsePushBroadcastReceiver (by inheriting from ParsePushBroadcastReceiver).

<receiver android:name=".receivers.CustomParseReceiver"
            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>

And

public class CustomParseReceiver extends ParsePushBroadcastReceiver {

   @Override
    protected Notification getNotification(Context context, Intent intent) {
        Log.i("PushNotifications","PARSE getNotification");
        return null;
    }

    @Override
    protected void onPushOpen(Context context, Intent intent) {
        Log.i("PushNotifications","PARSE onPushOpen");
    }

    @Override
    protected void onPushReceive(Context context, Intent intent) {
        Log.i("PushNotifications","PARSE onPushReceive");        
    }

    private JSONObject getDataFromIntent(Intent intent) {
        Log.i("PushNotifications","PARSE getDataFromIntent");            
    }

}

It did not work either.

The thing is.. when I create my own GCM BroadcastReceiver and I send a notification from my own server (with some small php script), the notification is successfully received.

I really wonder what's wrong with my implementation of the client side parse notifications system.

Any hint on where the problem might come from?

like image 849
AlexMok Avatar asked May 28 '15 12:05

AlexMok


2 Answers

My 2 cents on this (hope this helps):

I had a similar issue, which I resolved (Explained Here), so that might be the issue, if not, have a look at my files, at a quick glance they seem to have some small difference from yours, and they work so it's worth a look:

Manifest section:

   <!-- for parse pushes -->
    <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.MyStuff.stuff.utils.MyPushReceiver"
            android:exported="false" >
        <intent-filter>
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.OPEN" />
            <action android:name="com.parse.push.intent.DELETE" />
        </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" />

            <category android:name="com.MyStuff.stuff" />
        </intent-filter>
    </receiver>
    <meta-data
            android:name="com.parse.push.notification_icon"
            android:resource="@drawable/ic_launcher" />

I implemented my own ParsePushBroadcastReceiver:

public class MyPushReceiver extends ParsePushBroadcastReceiver
{
    @Override
    protected void onPushOpen(Context context, Intent intent)
    {
     //bla bla...
    }

    @Override
    protected void onPushReceive(Context context, Intent intent)
    {
     // bla bla...
    }

    @Override
    protected Notification getNotification(Context context, Intent intent)
    {
        Notification notification = super.getNotification(context, intent);
     // do some stuff with it..
    }

}

and my Application class:

public class MyApplication extends Application
{

    private static final String CKey = "***********";
    private static final String AId = "************";

    @Override
    public void onCreate()
    {
        super.onCreate();
        Parse.initialize(this, AId, CKey);

        //initialized some stuff..

        ParsePush.subscribeInBackground("", new SaveCallback()
        {
            @Override
            public void done(ParseException e)
            {
                if (null == e)
                {
                    ParseInstallation install = ParseInstallation.getCurrentInstallation();
                    String token = install.getString("deviceToken");
                   //do some stuff with it...
                }
            }
        });
        ParseInstallation.getCurrentInstallation().saveInBackground();
        //some more stuff
    }

Hope it sets you on the right path!

like image 71
TommySM Avatar answered Nov 12 '22 13:11

TommySM


first : did you enable Push Notification Settings From Your project setting

I faced the same problem , finally i delete my project and build new one in Parse , then the problem solved without doing any more things !!!! Why ?? I dont Know ...

and dont forget to get always the latest Parse SDK,, now its Parse-1.9.2.jar

like image 36
Maher Ismaail Avatar answered Nov 12 '22 12:11

Maher Ismaail