我正在我的应用程序中实现解析通知,这是我第一次使用推送通知.
我一直在关注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.onRtlPropertIEsChanged05-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)V05-27 16:41:06.041 1023-1023/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x000705-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.getChangingConfigurations05-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 ()I05-27 16:41:06.041 1023-1023/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000205-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.getType05-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)I05-27 16:41:06.041 1023-1023/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000205-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 15ms05-27 16:41:06.145 1023-1023/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so05-27 16:41:06.145 1023-1023/com.example.pchakraverti.pushnotification D/﹕ HostConnection::get() New Host Connection established 0xb7b8f838, tID 102305-27 16:41:06.153 1023-1023/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so05-27 16:41:06.153 1023-1023/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so05-27 16:41:06.189 1023-1023/com.example.pchakraverti.pushnotification W/EGL_genymotion﹕ eglSurfaceAttrib not implemented05-27 16:41:06.193 1023-1023/com.example.pchakraverti.pushnotification D/Openglrenderer﹕ Enabling deBUG mode 005-27 16:41:06.257 1023-1023/com.example.pchakraverti.pushnotification D/Openglrenderer﹕ TextureCache::get: create texture(0xb7c07868): name, size, mSize = 2, 4096, 409605-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 14ms05-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 12ms05-27 16:41:25.853 1023-1023/com.example.pchakraverti.pushnotification I/TAG﹕ Push Received05-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.onRtlPropertIEsChanged05-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)V05-27 16:44:24.105 1108-1108/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x000705-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.getChangingConfigurations05-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 ()I05-27 16:44:24.105 1108-1108/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000205-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.getType05-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)I05-27 16:44:24.105 1108-1108/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000205-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 2ms05-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.so05-27 16:44:24.269 1108-1108/com.example.pchakraverti.pushnotification D/﹕ HostConnection::get() New Host Connection established 0xb7c07110, tID 110805-27 16:44:24.273 1108-1108/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so05-27 16:44:24.273 1108-1108/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so05-27 16:44:24.317 1108-1108/com.example.pchakraverti.pushnotification W/EGL_genymotion﹕ eglSurfaceAttrib not implemented05-27 16:44:24.325 1108-1108/com.example.pchakraverti.pushnotification D/Openglrenderer﹕ Enabling deBUG mode 005-27 16:44:24.373 1108-1108/com.example.pchakraverti.pushnotification D/Openglrenderer﹕ TextureCache::get: create texture(0xb7c07868): name, size, mSize = 2, 4096, 409605-27 16:44:28.565 1108-1108/com.example.pchakraverti.pushnotification D/Openglrenderer﹕ TextureCache::flush: target size: 245705-27 16:44:28.565 1108-1108/com.example.pchakraverti.pushnotification D/Openglrenderer﹕ TextureCache::callback: name, removed size, mSize = 2, 4096, 005-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 implemented05-27 16:44:52.969 1108-1108/com.example.pchakraverti.pushnotification D/Openglrenderer﹕ TextureCache::get: create texture(0xb7c07868): name, size, mSize = 6, 4096, 409605-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.onRtlPropertIEsChanged05-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)V05-27 16:54:10.253 1267-1267/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x000705-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.getChangingConfigurations05-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 ()I05-27 16:54:10.257 1267-1267/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000205-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.getType05-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)I05-27 16:54:10.257 1267-1267/com.example.pchakraverti.pushnotification D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000205-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 23ms05-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.so05-27 16:54:10.353 1267-1267/com.example.pchakraverti.pushnotification D/﹕ HostConnection::get() New Host Connection established 0xb7c59dd8, tID 126705-27 16:54:10.361 1267-1267/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so05-27 16:54:10.361 1267-1267/com.example.pchakraverti.pushnotification D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so05-27 16:54:10.397 1267-1267/com.example.pchakraverti.pushnotification W/EGL_genymotion﹕ eglSurfaceAttrib not implemented05-27 16:54:10.405 1267-1267/com.example.pchakraverti.pushnotification D/Openglrenderer﹕ Enabling deBUG mode 005-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摘录中记录了一个“推送接收”.
但是之后,我什么也收不到!
到底是怎么回事 ?
更新资料
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.access00(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.access00(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)
解决方法:
通常,我建议使用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) {}});
总结 以上是内存溢出为你收集整理的Java-无法在Android中接收解析推送通知全部内容,希望文章能够帮你解决Java-无法在Android中接收解析推送通知所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)