【问题标题】:I can't receive push notifications in app from Parse我无法在应用程序中接收来自 Parse 的推送通知
【发布时间】:2014-12-08 21:33:43
【问题描述】:

我在我的应用程序中配置了 Parse API,除了推送通知之外一切正常。我试图从网站发送它们,但它们没有到达应用程序。 我按照documentation 中的说明完成了所有操作,但我无法接收通知推送。

我能够收到一个,但当我按下它时,应用程序崩溃了。现在我试图改变一些东西,但即使倒车我也不能再收到它们了。我该如何解决我的问题?

编辑:由于某些用户可能感兴趣,以下是我用于推送通知的部分代码:

主类(虽然不是 MainActivity):

    public class InstantsApplication extends Application {
public void onCreate() {
    super.onCreate();
    // Hidden
    Parse.initialize(this, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");



    ParseInstallation.getCurrentInstallation().saveInBackground();

    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);
            }
        }
     }
}

AndroidManifest(此处未包括权限,相信我已将它们放入):

<!-- PARSE -->
<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.
        -->
        <!-- I have hidden package name root for this question -->
        <category android:name="com.xxxxxxxx.instants" />


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

【问题讨论】:

    标签: java android parse-platform push-notification


    【解决方案1】:

    我之前遇到过这个问题,那是因为你使用了最新的 Parse api。 您只需要进行一些更改。

    首先,要修复从 Parse 后端发出推送直接导致的错误,您需要在 Manifest 中声明 Parse 推送的通知图标。

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

    在关闭应用程序标签之前使用。

    现在从后端发出另一个推送将按您的预期为您提供推送通知。到现在为止还挺好。点击推送会再次导致应用崩溃。要解决此问题,您需要删除现已弃用的调用 PushService.setDefaultPushCallback(...) 并添加您自己的 Receiver 类。我在 *.util 包中执行此操作,如下所示:

    public class Receiver extends ParsePushBroadcastReceiver {
    
        @Override
        public void onPushOpen(Context context, Intent intent) {
            Intent i = new Intent(context, MainActivity.class);
            i.putExtras(intent.getExtras());
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
    

    接下来,将默认 Parse 接收器更改为刚刚创建的接收器:- 转到您的 Manifest 文件。

     <receiver
                android:name="your.package.name.utils.Receiver"
                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>
    

    【讨论】:

    • 很有帮助,但你告诉了我所有我已经尝试过和逆转的事情。我仍然没有收到推送通知(反正我已经设置了图标但这里没有提到)
    • 你的日志猫上显示了什么?我真的试过了,它会显示D/com.parse.push:成功订阅广播频道。
    • 我也是,但还是没有收到通知
    • 我实际上卸载并再次运行了该应用程序,现在可以工作了。我不知道为什么有时会这样。感谢您的帮助,并证实了我对接收器的怀疑
    • 您使用的是什么设备?你试过模拟器吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    相关资源
    最近更新 更多