未收到解析 Android 通知
Posted
技术标签:
【中文标题】未收到解析 Android 通知【英文标题】:Parse Android Notification not received 【发布时间】:2016-01-06 19:40:37 【问题描述】:我按照教程在 android 上解析通知 https://parse.com/apps/quickstart#parse_push/android/native/existing
但我仍然无法在我的应用程序中收到通知,这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.walid.app_247_parse">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:protectionLevel="signature"
android:name="com.walid.app_247_parse.C2D_MESSAGE" />
<uses-permission android:name="com.walid.app_247_parse.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".UserPhoneNumber" />
<activity android:name=".MainPage" />
<activity android:name=".ChatView" />
<activity android:name=".Connect_Main" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyAZmjxQ_QZRG-0m-oMNcl0zC0H_eOEB8Jw" />
<service android:name="com.parse.PushService" />
<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" />
<category android:name="com.walid.app_247_parse" />
</intent-filter>
</receiver>
</application>
我还在我的 Launcher Activity 中添加了这些行
Parse.initialize(this, getResources().getString(R.string.ParseAppID), getResources().getString(R.string.ParseClientID));
ParseInstallation.getCurrentInstallation().saveInBackground();
ParsePush.subscribeInBackground("247APP", new SaveCallback()
@Override
public void done(com.parse.ParseException e)
Log.e("PARSE", "Successfully subscribed to Parse!");
);
而且我可以看到成功订阅的日志,而且每当我从 parse.com 发送通知时,我的设备中仍然没有显示任何内容。 请注意,我正在运行 Android 5 的 Asus Zenfone 2 上测试该应用程序。
【问题讨论】:
发布ParsePushBroadcastReceiver
代码
【参考方案1】:
从 Application 类的 onCreate 方法调用 Parse.initialize(而不是 Launcher Activity)
AndroidManifest.xml
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
MyApplication.java
public class MyApplication extends Application
@Override
public void onCreate()
super.onCreate();
// Enable Local Datastore.
Parse.enableLocalDatastore(this);
// Add your initialization code here
Parse.initialize(this, getResources().getString(R.string.ParseAppID), getResources().getString(R.string.ParseClientID));
// Subscribe to a channel
ParsePush.subscribeInBackground("twofourseven", new SaveCallback()
@Override
public void done(ParseException e)
if (e == null)
Log.d("PARSE", "Successfully subscribed to Parse channel");
else
Log.d("PARSE", "Failed to subscribe to Parse channel");
);
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
// Optionally enable public read access.
// defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
编辑:
包名+“.permission.C2D_MESSAGE”(缺少.permission)
<permission android:protectionLevel="signature" android:name="com.walid.app_247_parse.permission.C2D_MESSAGE" />
<uses-permission android:name="com.walid.app_247_parse.permission.C2D_MESSAGE" />
【讨论】:
我按照你的代码,频道名称“247APP”有问题,因为它以数字开头,我把它改成twofourseven,现在它显示“成功订阅Parse频道”但是它仍然无法收到通知,请注意我的应用代码中有用户注册解析和登录,谢谢 是否缺少改变
Log.e("PARSE", "Successfully subscribed to Parse!");
到
if (e==null)
Log.e("Parse", "Success");
else
Log.e("PARSE", e.toString());
查看错误是什么,或者该部分是否确实成功。
【讨论】:
当您对您的解决方案感到满意时,不要忘记设置为问题已回答/完成!以上是关于未收到解析 Android 通知的主要内容,如果未能解决你的问题,请参考以下文章