【问题标题】:Android Studio: App does not receive push notifications from parse.comAndroid Studio:应用没有收到来自 parse.com 的推送通知
【发布时间】:2015-04-14 12:20:43
【问题描述】:

我知道这个问题已经被问过很多次了,但我仍然无法从 parse.com 获得推送通知

从 parse.com 成功发送推送通知,但我的应用没有收到它们。

大多数人都使用以下方法解决了他们的问题:I can't receive push notifications in app from Parse bt 这对我不起作用。

我也试过Android - Can not Receive Push from Parse.com

有些人建议更改软件包的名称,我这样做了,甚至制作了一个新应用,但没有帮助。

注意:我已经导入了 parse-1.9.1.jar 和 bolts-android-1.2.0.jar

这是我的 MainActivity

package com.example.user.parsetest;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import com.parse.Parse;
public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Parse.initialize(this, "********", "********");

}
}

我尝试按照许多答案中的建议添加自定义 PushReceiver

package com.example.user.parsetest;

import android.app.Activity;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import com.parse.ParsePushBroadcastReceiver;

public class Receiver extends ParsePushBroadcastReceiver {

@Override
protected Notification getNotification(Context context, Intent intent) {
    // TODO Auto-generated method stub
    return super.getNotification(context, intent);
}

@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);
}
@Override
protected void onPushReceive(Context context, Intent intent) {
    //here You can handle push before appearing into status e.g if you want to stop it.
    super.onPushReceive(context, intent);

}
}

还有 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/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.example.user.parsetest.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>
    <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.starter" to match your app's package name.
            -->
            <category android:name="com.example.user.parsetest" />
        </intent-filter>
    </receiver>
    <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.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <meta-data android:name="com.parse.push.notification_icon"
        android:resource="@drawable/ic_launcher"/>

    <!--
      IMPORTANT: Change "com.parse.starter.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.user.parsetest.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.example.user.parsetest.permission.C2D_MESSAGE" />
</application>

无法找出问题所在。请帮忙。

【问题讨论】:

  • 你有什么错误吗?
  • 首先在 Parse 控制台上检查通知是否发送正确。其次从清单中删除默认接收者的条目,即 ......
  • 只是一个疯狂的猜测......但尝试使用 Parse-1.8.2 jar 和 bolts-android-1.1.4.jar?这种组合(以前的版本)似乎对我有用
  • @Napolean.. 是的 Parse 控制台说通知已经发送.. 但是有 0 个接收者.. 从清单中删除条目也没有帮助
  • @droidx.. 我怎样才能下载 parse1.8.2 jar 文件?因为官方SDK有1.9.1版

标签: android android-studio parse-platform push-notification


【解决方案1】:

在您的应用程序类中 Parse.initialize(appkey,clientkey); 行之后只需添加以下行,它应该可以工作。

// Current installation object
ParseInstallation parseInstallation = ParseInstallation.getCurrentInstallation();

// Save the updated installation object
parseInstallation.saveInBackground(new SaveCallback() {
    @Override
    public void done(ParseException e) {
        MyLog.e(MainApplication.class.getSimpleName(), "Installation object saved "+((e!=null)?"failed":"successfully"));
    }
 });

【讨论】:

  • 还是一样..推送已发送..未收到:(
  • 我几天前确实添加了这个项目。你可以尝试运行这个示例项目github.com/Keshava11/parsecheck 并在 ParseCheckApplication 类中用你替换键。
  • @Napolean..parecheck 项目不在 eclipse 中运行。即它在启动后崩溃,在 Android Studio 中,出现找不到插件 com.android.application 的错误
  • 您在 Android Studio 中是否有任何现有项目。在该项目中执行 File-> Import module -> this module
  • @Napolean 我也遇到了同样的问题?
【解决方案2】:

尝试下载这个项目:https://www.dropbox.com/s/fpp22fi2o7c8vai/PushParse.zip?dl=0

它包含 1.4.3 lib jar 文件。它已经过时了,但它正在工作。

我做了一些调整,设法将其更新到最新的库 (1.9.2)。

注意出现在您的 logcat 中的错误。我得到了这个:error setting up push notification using parse.com and android studio

如果通过确保我的 android 清单非常干净和有条理来解决。遵循结构。

这是我更新的解析库的代码:

public class ParsePushNotification extends Application {

    public ParsePushNotification() {
    }


    public void onCreate(){
        super.onCreate();

        Log.d("!!!", "This was called. Parse");
        Parse.initialize(this, "paste your app id here", "paste your client id her");
//        PushService.setDefaultPushCallback(this, LoginActivity.class);
//        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);
                }
            }
        });
    }
}

该类应在您的清单中使用:

<application
      android:name="com.example.app.ParsePushNotification"

这是我的接收器类:

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

这是我的清单:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <permission android:name="com.example.app.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.app.permission.C2D_MESSAGE" />

<application
        android:name="com.example.app.ParsePushNotification"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.NoTitle">

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

    <activity
        android:name=".LoginActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <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" />
            <category android:name="com.example.app" />
        </intent-filter>
    </receiver>
    <receiver android:name="com.example.app.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>
</application>

祝你好运!

【讨论】:

  • 如何让 onPushOpen() 工作?每当我通过控制台发送推送时,我都无法看到通知笔。
  • “通过控制台”是什么意思?请尝试下载示例项目以测试您的推送通知。
  • 可能在你注册你的应用时,它把对象id保存在了安装类中。然后你删除了它,但你没有卸载应用程序?因为它发生在我身上几次,因为它不再注册,所以我的应用无法接收推送。尝试卸载该应用程序,然后再次安装它,看看是否在 Parse Core 的安装类中创建了一个新行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多