在 Android 中未收到解析推送通知
Posted
技术标签:
【中文标题】在 Android 中未收到解析推送通知【英文标题】:Not receiving Parse Push Notifications In Android 【发布时间】:2015-05-27 15:33:37 【问题描述】:我正在我的应用中实现解析通知,这是我第一次使用推送通知。
我一直在关注 this 教程以了解解析的工作原理!
然后我发现PushService.setDefaultPushCallback(this, MainActivity.class);
已被弃用并使用this 进行指导。
我的代码如下:
androidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pchakraverti.pushnotification" >
<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.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" />
<!--
IMPORTANT: Change "com.parse.tutorials.pushnotifications.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
android:name="com.example.pchakraverti.pushnotification.permission.C2D_MESSAGE" />
<uses-permission android:name="com.example.pchakraverti.pushnotification.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<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.tutorials.pushnotifications" to match your app's package name.
-->
<category android:name="com.example.pchakraverti.pushnotification" />
</intent-filter>
</receiver>
<receiver android:name="com.example.pchakraverti.pushnotification.MyBroadcastReceiver" 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>
</application>
</manifest>
MyBroadcastReceiver.java:
package com.example.pchakraverti.pushnotification;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.parse.ParsePushBroadcastReceiver;
/**
* Created by PChakraverti on 5/27/2015.
*/
public class MyBroadcastReceiver extends ParsePushBroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
Log.i("TAG", "Push Received");
/*Intent launchIntent = new Intent(context, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, launchIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("Push Notification")
.setContentText("hello")
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, notification);*/
MainActivity.java:
package com.example.pchakraverti.pushnotification;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseInstallation;
import com.parse.ParsePush;
import com.parse.SaveCallback;
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Parse.initialize(this, "7Wm4v4FP28FHPW06zVBj6Ifcc8QeQObr3LUycs90", "U90b8QH4gLOXGfZLwBSqGqOZSo5GFLiu9sRi4bxW");
ParsePush.subscribeInBackground("", new SaveCallback()
@Override
public void done(ParseException e)
if (e == null)
Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
else
Log.e("com.parse.push", "failed to subscribe for push", e);
);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
当我从 parse.com 发送推送时,它说成功,但我的 android 设备上没有收到任何东西。
编辑
我已经根据官方教程更新了代码。 但我还是有这个问题。
我还是错过了什么吗?
编辑
日志猫:
/com.example.pchakraverti.pushnotification I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
05-27 16:41:06.041 1023-1023/com.example.pchakraverti.pushnotification W/dalvikvm﹕ VFY: unable to resolve virtual method 13226: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
05-27 16:41:06.041 1023-1023/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
05-27 16:41:06.041 1023-1023/com.example.pchakraverti.pushnotification I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
05-27 16:41:06.041 1023-1023/com.example.pchakraverti.pushnotification W/dalvikvm﹕ VFY: unable to resolve virtual method 450: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
05-27 16:41:06.041 1023-1023/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-27 16:41:06.041 1023-1023/com.example.pchakraverti.pushnotification I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
05-27 16:41:06.041 1023-1023/com.example.pchakraverti.pushnotification W/dalvikvm﹕ VFY: unable to resolve virtual method 472: Landroid/content/res/TypedArray;.getType (I)I
05-27 16:41:06.041 1023-1023/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-27 16:41:06.089 1023-1027/com.example.pchakraverti.pushnotification D/dalvikvm﹕ GC_CONCURRENT freed 199K, 3% free 10928K/11207K, paused 11ms+1ms, total 15ms
05-27 16:41:06.145 1023-1023/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
05-27 16:41:06.145 1023-1023/com.example.pchakraverti.pushnotification D/﹕ HostConnection::get() New Host Connection established 0xb7b8f838, tid 1023
05-27 16:41:06.153 1023-1023/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
05-27 16:41:06.153 1023-1023/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
05-27 16:41:06.189 1023-1023/com.example.pchakraverti.pushnotification W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
05-27 16:41:06.193 1023-1023/com.example.pchakraverti.pushnotification D/OpenGLRenderer﹕ Enabling debug mode 0
05-27 16:41:06.257 1023-1023/com.example.pchakraverti.pushnotification D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb7c07868): name, size, mSize = 2, 4096, 4096
05-27 16:41:06.629 1023-1027/com.example.pchakraverti.pushnotification D/dalvikvm﹕ GC_CONCURRENT freed 247K, 4% free 11077K/11463K, paused 12ms+0ms, total 14ms
05-27 16:41:07.413 1023-1023/com.example.pchakraverti.pushnotification D/com.parse.push﹕ successfully subscribed to the broadcast channel.
05-27 16:41:08.385 1023-1027/com.example.pchakraverti.pushnotification D/dalvikvm﹕ GC_CONCURRENT freed 352K, 5% free 11163K/11655K, paused 11ms+0ms, total 12ms
05-27 16:41:25.853 1023-1023/com.example.pchakraverti.pushnotification I/TAG﹕ Push Received
05-27 16:44:24.105 1108-1108/com.example.pchakraverti.pushnotification I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
05-27 16:44:24.105 1108-1108/com.example.pchakraverti.pushnotification W/dalvikvm﹕ VFY: unable to resolve virtual method 13226: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
05-27 16:44:24.105 1108-1108/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
05-27 16:44:24.105 1108-1108/com.example.pchakraverti.pushnotification I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
05-27 16:44:24.105 1108-1108/com.example.pchakraverti.pushnotification W/dalvikvm﹕ VFY: unable to resolve virtual method 450: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
05-27 16:44:24.105 1108-1108/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-27 16:44:24.105 1108-1108/com.example.pchakraverti.pushnotification I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
05-27 16:44:24.105 1108-1108/com.example.pchakraverti.pushnotification W/dalvikvm﹕ VFY: unable to resolve virtual method 472: Landroid/content/res/TypedArray;.getType (I)I
05-27 16:44:24.105 1108-1108/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-27 16:44:24.185 1108-1111/com.example.pchakraverti.pushnotification D/dalvikvm﹕ GC_CONCURRENT freed 224K, 3% free 10934K/11271K, paused 1ms+0ms, total 2ms
05-27 16:44:24.193 1108-1108/com.example.pchakraverti.pushnotification D/com.parse.push﹕ successfully subscribed to the broadcast channel.
05-27 16:44:24.269 1108-1108/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
05-27 16:44:24.269 1108-1108/com.example.pchakraverti.pushnotification D/﹕ HostConnection::get() New Host Connection established 0xb7c07110, tid 1108
05-27 16:44:24.273 1108-1108/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
05-27 16:44:24.273 1108-1108/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
05-27 16:44:24.317 1108-1108/com.example.pchakraverti.pushnotification W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
05-27 16:44:24.325 1108-1108/com.example.pchakraverti.pushnotification D/OpenGLRenderer﹕ Enabling debug mode 0
05-27 16:44:24.373 1108-1108/com.example.pchakraverti.pushnotification D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb7c07868): name, size, mSize = 2, 4096, 4096
05-27 16:44:28.565 1108-1108/com.example.pchakraverti.pushnotification D/OpenGLRenderer﹕ TextureCache::flush: target size: 2457
05-27 16:44:28.565 1108-1108/com.example.pchakraverti.pushnotification D/OpenGLRenderer﹕ TextureCache::callback: name, removed size, mSize = 2, 4096, 0
05-27 16:44:52.853 1108-1108/com.example.pchakraverti.pushnotification D/com.parse.push﹕ successfully subscribed to the broadcast channel.
05-27 16:44:52.953 1108-1108/com.example.pchakraverti.pushnotification W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
05-27 16:44:52.969 1108-1108/com.example.pchakraverti.pushnotification D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb7c07868): name, size, mSize = 6, 4096, 4096
05-27 16:54:10.253 1267-1267/com.example.pchakraverti.pushnotification I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
05-27 16:54:10.253 1267-1267/com.example.pchakraverti.pushnotification W/dalvikvm﹕ VFY: unable to resolve virtual method 13227: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
05-27 16:54:10.253 1267-1267/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
05-27 16:54:10.257 1267-1267/com.example.pchakraverti.pushnotification I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
05-27 16:54:10.257 1267-1267/com.example.pchakraverti.pushnotification W/dalvikvm﹕ VFY: unable to resolve virtual method 450: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
05-27 16:54:10.257 1267-1267/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-27 16:54:10.257 1267-1267/com.example.pchakraverti.pushnotification I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
05-27 16:54:10.257 1267-1267/com.example.pchakraverti.pushnotification W/dalvikvm﹕ VFY: unable to resolve virtual method 472: Landroid/content/res/TypedArray;.getType (I)I
05-27 16:54:10.257 1267-1267/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
05-27 16:54:10.309 1267-1270/com.example.pchakraverti.pushnotification D/dalvikvm﹕ GC_CONCURRENT freed 207K, 3% free 10919K/11207K, paused 22ms+0ms, total 23ms
05-27 16:54:10.321 1267-1267/com.example.pchakraverti.pushnotification D/com.parse.push﹕ successfully subscribed to the broadcast channel.
05-27 16:54:10.353 1267-1267/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
05-27 16:54:10.353 1267-1267/com.example.pchakraverti.pushnotification D/﹕ HostConnection::get() New Host Connection established 0xb7c59dd8, tid 1267
05-27 16:54:10.361 1267-1267/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
05-27 16:54:10.361 1267-1267/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
05-27 16:54:10.397 1267-1267/com.example.pchakraverti.pushnotification W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
05-27 16:54:10.405 1267-1267/com.example.pchakraverti.pushnotification D/OpenGLRenderer﹕ Enabling debug mode 0
05-27 16:54:10.469 1267-1267/com.example.pchakraverti.pushnotification D/OpenGLRenderer﹕ TextureCache::get: create texture(0xb7c07868): name, size, mSize = 2, 4096, 4096
可以看到logcat提取中记录了一个“Push Received”。
但在那之后,我再也没有收到任何东西了!
发生了什么事?
更新
05-27 13:47:20.888 1500-1500/com.example.pchakraverti.pushnotification E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.pchakraverti.pushnotification, PID: 1500
java.lang.RuntimeException: Unable to start receiver com.parse.ParseBroadcastReceiver: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(Context) before using the Parse library.
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2586)
at android.app.ActivityThread.access$1700(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(Context) before using the Parse library.
at com.parse.Parse.checkContext(Parse.java:448)
at com.parse.Parse.getApplicationContext(Parse.java:267)
at com.parse.ManifestInfo.getContext(ManifestInfo.java:324)
at com.parse.ManifestInfo.getPackageManager(ManifestInfo.java:328)
at com.parse.ManifestInfo.getPackageInfo(ManifestInfo.java:358)
at com.parse.ManifestInfo.deviceSupportsGcm(ManifestInfo.java:446)
at com.parse.ManifestInfo.getPushType(ManifestInfo.java:212)
at com.parse.PushService.startServiceIfRequired(PushService.java:222)
at com.parse.ParseBroadcastReceiver.onReceive(ParseBroadcastReceiver.java:19)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2579)
at android.app.ActivityThread.access$1700(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
【问题讨论】:
我在 Parse 中遇到了类似的问题。它可以在某些设备上运行,而在其他设备上却不行。在经历了数周的挫折之后,我改用了亚马逊的 AWS 推送通知,到目前为止它运行良好。 事情是在亚马逊的 AWS 中注册需要信用卡信息,我没有。这个问题的一些解决方案会很有帮助! 【参考方案1】:一般来说,我推荐official tutorial。
您的com.parse.ParseBroadcastReceiver
使用了错误的操作名称,应为:
<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.tutorials.pushnotifications" to match your app's package name.
-->
<category android:name="com.parse.tutorials.pushnotifications" />
</intent-filter>
</receiver>
您必须通过订阅频道来启用推送通知,例如,通过:
ParsePush.subscribeInBackground("", new SaveCallback()
@Override
public void done(ParseException e)
);
【讨论】:
我已经根据您的帖子和官方教程更新了代码。但我仍然遇到问题。我又错过了什么吗? 我添加了logcat,请检查一下。 你又发推送通知了吗? 是的。我又发送了 4 条通知,但没有一条到达我的设备 为什么要覆盖 onReceive 而不是 onPushReceive?【参考方案2】:还有一个常见原因可能是使用 GCM 控制台形式的服务器 API 密钥。 而应使用 Firebase 项目的 API 密钥。
【讨论】:
以上是关于在 Android 中未收到解析推送通知的主要内容,如果未能解决你的问题,请参考以下文章