Android上的通知解析无法工作

10

我一直在尝试设置安卓上的 Parse 推送通知。我遵循了不同的教程,并且试图从他们的 Web 平台发送通知,但是似乎没有什么作用。以下是我迄今为止尝试过的方法:

这是我的 Application 类的 onCreate 方法:

@Override
    public void onCreate() {
        super.onCreate();
        Log.i("PushNotifications","PARSE initialize");

        Parse.initialize(this, "******************************", "*****************************");
        ParseInstallation.getCurrentInstallation().saveInBackground();

        ParsePush.subscribeInBackground("", new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    Log.i("PushNotifications","com.parse.push" + " successfully subscribed to the broadcast channel.");
                } else {
                    Log.i("PushNotifications","com.parse.push" + " failed to subscribe for push");
                }
            }
        });

    }

我成功调用了此函数,并获得了日志记录。

已成功订阅广播频道。

以下是我的清单文件中一些相关内容:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<permission android:name="com.my.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.my.app.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<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.GET_ACCOUNTS" />

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:name=".App"
    android:theme="@style/AppTheme">

    <activity
        android:name=".activities.MainActivity"
        android:launchMode="standard"
        android:screenOrientation="sensorPortrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data android:name="com.parse.push.gcm_sender_id" android:value="id:123456789" />
    <meta-data android:name="com.parse.push.notification_icon" android:resource="@drawable/ic_launcher"/>

    <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.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>
    <!--com.parse.GcmBroadcastReceiver"-->
    <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" />
            <category android:name="com.my.app" />
        </intent-filter>
    </receiver>

</application>

当我尝试通过Parse网站发送推送时,没有任何反应。由于这种方法不起作用,我尝试实现自己的GcmBroadcastReceiver并在清单中进行更改。
<receiver android:name=".receivers.MyCustomReceiver"
            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" />
                <category android:name="com.my.app" />
            </intent-filter>
</receiver>

并且

public class MyCustomReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("PushNotifications","MyCustomReceiver");
    }
}

没有成功。

然后我还尝试创建自己的ParsePushBroadcastReceiver(通过继承ParsePushBroadcastReceiver)。

<receiver android:name=".receivers.CustomParseReceiver"
            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>

"同时,"
public class CustomParseReceiver extends ParsePushBroadcastReceiver {

   @Override
    protected Notification getNotification(Context context, Intent intent) {
        Log.i("PushNotifications","PARSE getNotification");
        return null;
    }

    @Override
    protected void onPushOpen(Context context, Intent intent) {
        Log.i("PushNotifications","PARSE onPushOpen");
    }

    @Override
    protected void onPushReceive(Context context, Intent intent) {
        Log.i("PushNotifications","PARSE onPushReceive");        
    }

    private JSONObject getDataFromIntent(Intent intent) {
        Log.i("PushNotifications","PARSE getDataFromIntent");            
    }

}

也没有起作用。

问题是,当我创建自己的GCM BroadcastReceiver并从自己的服务器发送通知(使用一些小型php脚本)时,通知被成功接收。

我真的很想知道客户端解析通知系统实现有什么问题。

请问可能出现问题的原因?


尝试在广播接收器上使用 android:exported="true" - FunkTheMonk
它抛出了一个SecurityException异常:为了防止外部篡改您应用程序的通知,所有注册处理以下操作的接收器必须将其导出属性设置为false:com.parse.push.intent.RECEIVE、com.parse.push.intent.OPEN、com.parse.push.intent.DELETE。 - AlexMok
1
你的代码看起来没问题。我认为你应该检查一下拼写错误。我曾经使用Parse,因为一个拼写错误 'Player' 'Players' 而卡了6个小时。 - Mayank
请更改CustomParseReceiver目录并将其添加到MainActivity包目录中,在添加后更新清单。 - Karan Maru
Alex,以下这行代码可能是引起问题的原因。你不需要定义发送方ID。删除它可能会修复你的问题。 <meta-data android:name="com.parse.push.gcm_sender_id" android:value="id:123456789" /> - Aashir
2个回答

3

以下是我的建议(希望能帮到你):

我曾遇到类似的问题,并通过解决(在这里有说明)解决了这个问题。如果不行,可以看一下我的文件,初步检查它们与你的文件有些小差异,但它们可以正常工作,所以值得一看:

清单部分:

   <!-- for parse pushes -->
    <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.MyStuff.stuff.utils.MyPushReceiver"
            android:exported="false" >
        <intent-filter>
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.OPEN" />
            <action android:name="com.parse.push.intent.DELETE" />
        </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" />

            <category android:name="com.MyStuff.stuff" />
        </intent-filter>
    </receiver>
    <meta-data
            android:name="com.parse.push.notification_icon"
            android:resource="@drawable/ic_launcher" />

我实现了自己的ParsePushBroadcastReceiver:

public class MyPushReceiver extends ParsePushBroadcastReceiver
{
    @Override
    protected void onPushOpen(Context context, Intent intent)
    {
     //bla bla...
    }

    @Override
    protected void onPushReceive(Context context, Intent intent)
    {
     // bla bla...
    }

    @Override
    protected Notification getNotification(Context context, Intent intent)
    {
        Notification notification = super.getNotification(context, intent);
     // do some stuff with it..
    }

}

和我的Application类:

public class MyApplication extends Application
{

    private static final String CKey = "***********";
    private static final String AId = "************";

    @Override
    public void onCreate()
    {
        super.onCreate();
        Parse.initialize(this, AId, CKey);

        //initialized some stuff..

        ParsePush.subscribeInBackground("", new SaveCallback()
        {
            @Override
            public void done(ParseException e)
            {
                if (null == e)
                {
                    ParseInstallation install = ParseInstallation.getCurrentInstallation();
                    String token = install.getString("deviceToken");
                   //do some stuff with it...
                }
            }
        });
        ParseInstallation.getCurrentInstallation().saveInBackground();
        //some more stuff
    }

希望这能为你指明正确的方向!

谢谢你的回答Tommy。我复制粘贴了你的代码,并将包替换为我的,但不幸的是它没有解决这个问题。关于从ParseInstallation对象获取的令牌,它是否与我使用GoogleCloudMessaging.getInstance(context).register()获取的GCM令牌相同? - AlexMok
据我所知,这是来自解析文档的内容:'deviceToken: 用于iOS设备的由苹果生成的令牌,或者由GCM用于跟踪注册ID的令牌(只读)',但我建议在他们的文档中进一步了解它 :) - TommySM

2

首先:您是否已经在项目设置中启用了推送通知设置?

我曾经遇到过同样的问题,最后我删除了我的项目并在Parse中创建了一个新项目,然后问题就解决了,而且不需要做任何其他事情!为什么?我不知道...

同时不要忘记始终获取最新的Parse SDK,现在是Parse-1.9.2.jar。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接