解析推送通知在 Genymotion 中有效,但在实际中无效 phone

Parse Push notification works in Genymotion, but doesn't work in real phone

我开发了一个使用解析推送通知服务的应用程序。 在整个过程中,我在 GenyMotion 上测试了该应用程序,同时将其与 android 工作室一起使用。

解析推送在那里工作正常。

但是现在开发完成后,当我在我的 Micromax Canvas A116 HD 运行 Android 4.1.2 中尝试相同的应用程序时,我根本没有收到任何推送通知。

我什至在广播接收器中设置了BP,它甚至一次都没有中断。

genyMotion 中的同一个应用 运行 如何收到通知。

以下是来自应用程序的代码:

Android 清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pchakraverti.enotice" >

    <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" />

    <permission android:protectionLevel="signature"
        android:name="com.example.pchakraverti.enotice.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.example.pchakraverti.enotice.permission.C2D_MESSAGE" />

    <application
        android:name="com.example.pchakraverti.enotice.InitParse"
        android:hardwareAccelerated="false"
        android:allowBackup="true"
        android:icon="@drawable/e_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".clgIdVerificationActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".StudentDetailsActivity"
            android:label="@string/title_activity_student_details"
            android:parentActivityName=".clgIdVerificationActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.pchakraverti.enotice.clgIdVerificationActivity" />
        </activity>
        <activity
            android:name=".NotificationActivity"
            android:label="@string/title_activity_notification"
            android:parentActivityName=".StudentDetailsActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.pchakraverti.enotice.StudentDetailsActivity" />
        </activity>
        <activity
            android:name=".WebViewActivity"
            android:label="@string/title_activity_web_view" >
        </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.enotice" />
            </intent-filter>
        </receiver>

        <receiver android:name="com.example.pchakraverti.enotice.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>

初始化解析:

package com.example.pchakraverti.enotice;

import android.app.Application;
import android.util.Log;
import android.widget.Toast;

import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseInstallation;
import com.parse.SaveCallback;

/**
 * Created by PChakraverti on 6/3/2015.
 */
public class InitParse extends Application {
    @Override
    public void onCreate(){
        super.onCreate();
        Parse.enableLocalDatastore(this);
        Parse.initialize(this, "KEY", "KEY");
        Toast.makeText(this, "Started", Toast.LENGTH_SHORT).show();
        /*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);
                }
            }
        });*/
        ParseInstallation.getCurrentInstallation().saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    Log.d("com.parse.push", "Ready for push.");
                } else {
                    e.printStackTrace();

                }
            }
        });
    }
}

我的广播接收者:

package com.example.pchakraverti.enotice;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.parse.ParsePushBroadcastReceiver;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Random;

import Database.Database;
import Database.NoticeInformation;

/**
 * Created by PChakraverti on 6/3/2015.
 */
public class MyBroadcastReceiver extends ParsePushBroadcastReceiver {
    @Override
    public void onPushReceive(Context context, Intent intent) {

        JSONObject json = null;

        Log.i("TAG", "Push Received");
        String data="", channel, action, f_name = "", n_id  = "0", n_type = "", n_head = "", n_file = "";
        try{
            action = intent.getAction();
            channel = intent.getExtras().getString("com.parse.Channel");
            data = intent.getExtras().getString("com.parse.Data");
            json = new JSONObject(data);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }

        try {
            n_type = json.getString("N_TYPE");
            n_head = json.getString("N_HEADER");
            n_file = json.getString("N_FILE");
        }catch (JSONException e){
            e.printStackTrace();
        }


        //Save Information to Database
        NoticeInformation ninfo = new NoticeInformation(0, n_type, n_head, +n_file, 0, 0);
        Database db = new Database(context);
        long r_id = db.insertIntoNoticeStorage(ninfo);

        Intent launchIntent = new Intent(context, WebViewActivity.class);
        launchIntent.putExtra("Link", n_file);
        launchIntent.putExtra("N_ID", String.valueOf(r_id));
        PendingIntent pi = PendingIntent.getActivity(context, new Random().nextInt(), launchIntent, PendingIntent.FLAG_CANCEL_CURRENT);


        Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.e_icon);

        Notification notification = new NotificationCompat.Builder(context)
                .setContentTitle("Push Notification")
                .setContentText("A New Notice Was Published.")
                .setContentIntent(pi)
                .setAutoCancel(true)
                .setLargeIcon(icon)
                .setSmallIcon(R.drawable.e_icon)
                .build();

        NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(1, notification);
    }
}

我根本无法理解发生了什么以及如何解决这个问题。 这与 android 版本有关吗?我没有使用最新 android 的任何设备,但在 Genymotion 上,该应用程序在 Android 4.1.1 中运行(收到推送)。

需要一些帮助!

你为什么评论 ParsePush.subscribeInBackground() ?? 如果您删除此评论,此代码应该有效。 我有一个应用程序的初始化和订阅方式完全相同。(确保你的应用程序设置上的选项 "Client push enabled" 是 "Yes")。

我找到了问题的解决方案。 我已经在 android 4.1.2、4.3.

中对此进行了测试

工作 class 给出如下:

package com.pchakraverti.enotice_new;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.NotificationCompat;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Random;

import database.*;

/**
 * Created by PChakraverti on 6/6/2015.
 */


public class MyBroadcastReceiver extends BroadcastReceiver {

    JSONObject json = null;

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.i("TAG", "Push Received");
        String data="", channel, action, n_id  = "0", n_type = "", n_head = "", n_file = "";;
        try{
            action = intent.getAction();
            channel = intent.getExtras().getString("com.parse.Channel");
            data = intent.getExtras().getString("com.parse.Data");
            json = new JSONObject(data);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }

        try {
            n_type = json.getString("N_TYPE");
            n_head = json.getString("N_HEADER");
            n_file = json.getString("N_FILE");
        }catch (JSONException e){
            e.printStackTrace();
        }

        NoticeInformation ninfo = new NoticeInformation(0, n_type, n_head, n_file, 0, 0);
        Database db = new Database(context);
        long r_id = db.insertIntoNoticeStorage(ninfo);

        Intent launchIntent = new Intent(context, WebViewActivity.class);
        launchIntent.putExtra("Link", n_file);
        launchIntent.putExtra("N_ID", String.valueOf(r_id));
        PendingIntent pi = PendingIntent.getActivity(context, new Random().nextInt(), launchIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.e_icon);

        Notification notification = new NotificationCompat.Builder(context)
                .setContentTitle("A notice has been published!")
                .setContentText(n_head)
                .setContentIntent(pi)
                .setAutoCancel(true)
                .setLargeIcon(icon)
                .setSmallIcon(R.drawable.e_icon)
                .build();

        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_LIGHTS;

        NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(1, notification);
    }
}

ParseBroacastReceiver 完全失败了。这怎么能完美地工作。