Search code examples
androidpush-notificationbroadcastreceiverparse-platformgoogle-cloud-messaging

BroadcastReceiver works on emulator not on device


I am using Parse to send notifications from device to device. I've set up a broadcast receiver to generate a custom notification when it receives intent from a notification coming in from Parse. The problem is that the broadcast receiver on the emulator works just fine but on the device it just does not receive the intent.

AndroidManifest

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />


<permission android:protectionLevel="signature"
    android:name="org.example.app.permission.C2D_MESSAGE" />
<uses-permission android:name="com.example.app.permission.C2D_MESSAGE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
    <activity
        android:name="org.example.app.MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleInstance">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <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.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="org.example.app" />
        </intent-filter>
</receiver>
    <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="org.example.app.Receiver"
        android:priority="999999999"
        android:exported="true"
        android:enabled="true">
        <intent-filter android:priority="999999999">
            <action android:name="org.example.app.A_CUSTOM_INTENT"/>

        </intent-filter>
    </receiver>

</application>

An this is the onClick that calls for a Parse notification

    @Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    Toast.makeText(context, "this is shown"     , Toast.LENGTH_LONG).show();

    try {

        ParseQuery<ParseInstallation> query = ParseInstallation.getQuery();
        JSONObject data = new JSONObject("{\"action\": \"org.example.app.A_CUSTOM_INTENT\"}");

        ParsePush androidPush = new ParsePush();
        androidPush.setData(data);
        androidPush.setExpirationTimeInterval(120);
        androidPush.setQuery(query);
        androidPush.sendInBackground();
        } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

As I've said, this works on an emulator just fine and the device does receive notifications from the Parse service but does not respond to intent. Thank you for your help.


Solution

  • Looks like you have an error here :

    <permission android:protectionLevel="signature"
        android:name="org.example.app.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.example.app.permission.C2D_MESSAGE" />
    

    I'm assuming the correct value is org.example.app (based on the rest of the manifest), so it should be :

    <permission android:protectionLevel="signature"
        android:name="org.example.app.permission.C2D_MESSAGE" />
    <uses-permission android:name="org.example.app.permission.C2D_MESSAGE" />
    

    The reason it works on the emulator and not on the device could be that the device has an older Android version. This error affects only 2.x Android devices.