过去两个小时我一直试图解决这个问题。我已经执行了初始化推送通知所需的所有步骤,但是甚至无法接收测试通知。
在我的应用程序的仪表板上,我确实看到一个名为" Installation"的类,其中包含用户的对象ID。
如果我尝试向所有设备发送推送通知,它会显示在推送日志中,但设备上没有任何注册。
要解决此问题,我尝试在模拟器中卸载并重新安装应用程序,清理项目,重新启动android studio并以其他用户身份登录(当我发送测试通知时,另一台设备已在Installation类中注册) 。没有任何效果。我附上以下相关代码。
public class MyApplicationName extends Application {
public static final String TAG = MyApplicationName.class.getSimpleName();
@Override
public void onCreate() {
Parse.initialize(this, "-", "-");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
//To register the installation to each user that logs in
public static void updateParseInstallation(ParseUser user) {
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put(ParseConstants.KEY_USER_ID, user.getObjectId());
installation.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e!= null) {
Log.d(TAG, e.getMessage());
}
}
});
}
}
public class LoginActivity extends Activity {
// some code
if (e == null) {
// login success!
MyApplicationName.updateParseInstallation(user);
// some other code
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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" />
<!--
IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
android:name="Package.Package1.C2D_MESSAGE" />
<uses-permission android:name="Package.Package1.permission.C2D_MESSAGE" />
<application
android:name=".Application"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
//other code
</activity>
<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" />
<!--
IMPORTANT: Change "com.parse.starter" to match your app's package name.
-->
<category android:name="Package.Package1" />
</intent-filter>
</receiver>
</application>
编辑
我添加了一些额外的代码来获取更多信息,我添加了以下代码,以及logcat的相关部分
public class MyApplicationName extends Application {
//code for initializing parse and ParsePush
//this static method will run everytime a user logs in
public static void updateParseInstallation(ParseUser user) {
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put(ParseConstants.KEY_USER_ID, user.getObjectId());
installation.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e!= null) {
Log.d(TAG, e.getMessage());
因此,只要用户登录,就会运行以下代码: MyApplicationName.updateParseInstallation(用户);
推送通知假设每次找到对手时都会运行:
protected void sendPushNotification() {
ParseQuery<ParseInstallation> query = ParseInstallation.getQuery();
query.whereMatches(ParseConstants.KEY_USER_ID, mOpponent.getObjectId());
ParsePush push = new ParsePush();
push.setQuery(query);
push.setMessage(getString
(R.string.opponent_found_notification,
ParseUser.getCurrentUser().getUsername()));
push.sendInBackground(new SendCallback() {
@Override
public void done(ParseException e) {
if (e != null) {
Log.d(TAG, e.getMessage());
以下是logcat的有趣部分:
09-18 14:32:28.290 2324-2358/com.teamtreehouse.ribbit E/com.parse.ParsePushRouter﹕ Ignoring PPNS push missing timestamp
09-18 14:41:18.580 8824-8831/Package.Package1/W/art﹕ Suspending all threads took: 10ms
09-18 14:47:28.500 2324-2358/com.teamtreehouse.ribbit E/com.parse.ParsePushRouter﹕ Ignoring PPNS push missing timestamp
09-18 15:01:14.660 1314-1350/system_process W/BatteryStatsImpl﹕ Couldn't get kernel wake lock stats
Ribbit是我在尝试推送当前应用程序之前正在练习发送推送通知的应用程序。我不知道为什么这个应用程序出现在日志中,但我怀疑这是推送通知无法正常工作的原因。我试着清理我的浏览器,重新启动android studio,但是我在log cat中得到了相同的消息。有人可以,请帮帮我吗?我已经在下面附上了整个日志猫,以防有人感兴趣。
09-18 14:32:28.140 1314-1314/system_process I/ActivityManager﹕ Killing 2301:com.android.exchange/u0a27 (adj 11): empty for 1800s
09-18 14:32:28.150 1314-1314/system_process I/ActivityManager﹕ Killing 2244:com.android.settings/1000 (adj 13): empty for 1804s
09-18 14:32:28.160 1314-1314/system_process I/ActivityManager﹕ Killing 2142:com.android.managedprovisioning/u0a8 (adj 13): empty for 1807s
09-18 14:32:28.170 1314-1314/system_process I/ActivityManager﹕ Killing 2114:com.android.dialer/u0a4 (adj 13): empty for 1807s
09-18 14:32:28.180 1314-1314/system_process I/ActivityManager﹕ Killing 1870:com.android.defcontainer/u0a3 (adj 13): empty for 1808s
09-18 14:32:28.190 1314-1314/system_process I/ActivityManager﹕ Killing 2004:com.android.music/u0a33 (adj 13): empty for 1812s
09-18 14:32:28.290 2324-2358/com.teamtreehouse.ribbit E/com.parse.ParsePushRouter﹕ Ignoring PPNS push missing timestamp
09-18 14:41:18.580 8824-8831/Package.Package1/W/art﹕ Suspending all threads took: 10ms
09-18 14:47:28.500 2324-2358/com.teamtreehouse.ribbit E/com.parse.ParsePushRouter﹕ Ignoring PPNS push missing timestamp
09-18 15:01:14.660 1314-1350/system_process W/BatteryStatsImpl﹕ Couldn't get kernel wake lock stats
09-18 15:02:00.030 1314-1340/system_process I/ProcessStatsService﹕ Prepared write state in 0ms
09-18 15:02:40.080 1314-1314/system_process V/BackupManagerService﹕ Running a backup pass
09-18 15:02:40.100 1314-1458/system_process V/PerformBackupTask﹕ Beginning backup of 7 targets
09-18 15:02:40.120 1556-1841/com.android.inputmethod.latin I/LatinIME:LogUtils﹕ Dictionary info: dictionary = UserHistoryDictionary.en_US ; version = 1441827702 ; date = 1442613760
09-18 15:02:40.160 1314-1458/system_process D/PerformBackupTask﹕ invokeAgentForBackup on @pm@
09-18 15:02:40.160 1314-1458/system_process V/BackupServiceBinder﹕ doBackup() invoked
09-18 15:02:40.180 2324-2358/com.teamtreehouse.ribbit E/com.parse.ParsePushRouter﹕ Ignoring PPNS push missing timestamp
09-18 15:02:40.200 1314-1458/system_process I/BackupRestoreController﹕ Getting widget state for user: 0
09-18 15:02:40.200 1314-1458/system_process I/PerformBackupTask﹕ no backup data written; not calling transport
09-18 15:02:40.200 1314-1458/system_process D/PerformBackupTask﹕ starting agent for backup of BackupRequest{pkg=com.android.dialer}
09-18 15:02:40.220 1314-1458 / system_process I / ActivityManager:启动proc 27097:com.android.dialer / u0a4备份com.android.dialer / .DialerBackupAgent 09-18 15:02:40.220 1314-1458 / system_process D / BackupManagerService:等待ApplicationInfo的代理{527c6ec com.android.dialer} 09-18 15:02:40.230 27097-27097 /? D / ExtensionsFactory:没有自定义扩展。
答案 0 :(得分:0)
这可能会帮助你
http://www.androidhive.info/2015/06/android-push-notifications-using-parse-com/
请避免使用gradle并查看Receiver和Manifest Files。
答案 1 :(得分:0)
查看this问题。这是我写的一个问题。复制我在问题中使用的代码,并按照接受的答案(也是我)的说明。
ParseApplication.java:
package your.package.name;
public class ParseApplication extends Application {
@Override
public void onCreate() {
Parse.initialize(this, "", "");
}
}