在Android中未收到解析推送通知

普里亚布拉塔

我正在我的应用程序中实现解析通知,这是我第一次使用推送通知。

我一直在关注教程,以了解语法的工作原理!

然后我发现了PushService.setDefaultPushCallback(this, MainActivity.class);已弃用,并用指导。

我的代码如下:

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摘录中记录了一个“推送接收”。

但是之后,我什么也收不到!

到底是怎么回事 ?

更新

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)
西格里

通常,我推荐官方教程

  1. 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>
    
  2. 您缺少此接收器(上面链接的教程中的代码):

    <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>
    
  3. 您必须通过订阅频道来启用推送通知,例如,通过:

    ParsePush.subscribeInBackground("", new SaveCallback() {
        @Override
        public void done(ParseException e) {}
    });
    

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Android上未收到推送通知

来自分类Dev

在Testflight中未收到推送通知

来自分类Dev

尚未收到在Back4App上带有解析的Android推送通知

来自分类Dev

Laravel Expo推送通知-在Android上未收到通知

来自分类Dev

Windows Phone应用未收到推送通知:解析

来自分类Dev

Android模拟器未收到推送通知

来自分类Dev

Android模拟器未收到推送通知

来自分类Dev

订阅后仍未收到Android Parse推送通知

来自分类Dev

互联网开启时未收到Android推送通知

来自分类Dev

在 IOS Firebase 云功能中未收到推送通知

来自分类Dev

Android设备未收到推送

来自分类Dev

未收到GCM推送通知(全部结束)

来自分类Dev

在睡眠模式下未收到推送通知

来自分类Dev

未收到GCM推送通知(全部结束)

来自分类Dev

设备未收到iOS推送通知

来自分类Dev

未收到Phonegap iOS推送通知

来自分类Dev

桌面Google Chrome推送通知未收到

来自分类Dev

未收到离子云推送GCM通知

来自分类Dev

未收到通知中的PendingIntent

来自分类Dev

独立Watchkitapp APNS推送通知已成功发送,但在Apple Watch中未收到通知

来自分类Dev

在使用Android的击剑中未收到任何通知

来自分类Dev

在android中“minifyEnabled true”后未收到通知,为什么?

来自分类Dev

iOS 使用cordova 推送通知。从未收到通知

来自分类Dev

GCM客户端未收到推送通知

来自分类Dev

GCM + AppEngine-应用程序未收到推送通知

来自分类Dev

iOS8.0.2上未收到Pushkit voip推送通知

来自分类Dev

iOS未收到通过API发送的Firebase推送通知

来自分类Dev

未收到推送通知-AWS SNS和Expo

来自分类Dev

使用应用程序时未收到iOS推送通知