我正在使用Parse从设备发送通知。我设置了一个广播接收器,当它收到来自Parse的通知的意图时生成一个自定义通知。问题是模拟器上的广播接收器工作正常,但在设备上它只是没有收到意图。
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>
这是调用Parse通知的onClick
@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();
}
}
正如我所说,这适用于模拟器,设备确实接收来自Parse服务的通知但不响应意图。谢谢您的帮助。
答案 0 :(得分:1)
看起来你在这里有错误:
<permission android:protectionLevel="signature"
android:name="org.example.app.permission.C2D_MESSAGE" />
<uses-permission android:name="com.example.app.permission.C2D_MESSAGE" />
我假设正确的值是org.example.app
(基于清单的其余部分),所以它应该是:
<permission android:protectionLevel="signature"
android:name="org.example.app.permission.C2D_MESSAGE" />
<uses-permission android:name="org.example.app.permission.C2D_MESSAGE" />
它在模拟器上运行而不在设备上的原因可能是该设备具有较旧的Android版本。此错误仅影响2.x Android设备。