如何在应用程序 运行 时在通知栏中显示解析推送通知?

How to show Parse Push Notification in notification bar while application is running?

我正在使用 Parse 进行推送通知,我 运行 遇到的问题是当我的应用程序 运行(在前台或后台)时 phone' s 操作系统不在通知栏中显示推送通知。我需要对我的实现进行哪些更改才能在通知栏上看到推送显示?

我的扩展应用程序 class 在 onCreate()

中有以下内容
// initialize Parse SDK
Parse.initialize(this, Constants.APPLICATION_ID_DEBUG, Constants.CLIENT_KEY_DEBUG);
ParsePush.subscribeInBackground(Constants.CHANNEL, new SaveCallback() {
    @Override
    public void done(ParseException e) {
        if (e == null) {
            Logger.i(TAG, "successfully subscribed to broadcast channel");
        } else {
            Logger.e(TAG, "failed to subscribe for push: " + e);
        }
    }
});
ParseInstallation.getCurrentInstallation().saveInBackground();

我的应用程序有一个登录系统,所以我使用登录用户的 ID 作为频道来订阅用户。因此,在我的应用程序的第一个 Activity 中,我在 onCreate().

中调用了以下代码片段
private void registerNotifications() {
        List<String> arryChannel = new ArrayList<String>();
        arryChannel.add(session.id);

        ParseInstallation parseInstallation = ParseInstallation.getCurrentInstallation();
        parseInstallation.put("channels", arryChannel);
        parseInstallation.saveEventually();
}

我还有一个正在工作的自定义接收器。每次发送推送时,都会由 onPushReceive 方法接收,但是,我希望推送显示在通知栏中。

public class ParsePushReceiver extends ParsePushBroadcastReceiver {
    private static final String TAG = ParsePushReceiver.class.getSimpleName();

    @Override
    public void onPushOpen(Context context, Intent intent) {
        Log.i(TAG, "onPushOpen");
    }

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

提前致谢!

只需删除 onPushReceive 方法,默认行为将保留(在状态栏中显示通知。 您之所以会出现此行为,是因为如果应用程序是 运行,则解析推送通知将调用不执行任何操作的方法 onPushReceive

我已经弄明白了。虽然 Sandra 提供的答案会让通知栏出现推送通知,但它并没有连接到 Parse。

NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!");

这会导致问题,因为如果您单击该通知,您创建的扩展 ParsePushBroadcastReceiver 的接收器将不会注册 onPushOpen。我对一切的实施​​都是正确的,我只需要添加

super.onPushReceive(context, intent);

这将使通知出现在通知栏上并记录点击次数。

因此请务必让您的接收器看起来像这样(至少)

public class ParsePushReceiver extends ParsePushBroadcastReceiver {
    private static final String TAG = ParsePushReceiver.class.getSimpleName();

    @Override
    public void onPushOpen(Context context, Intent intent) {
        Log.i(TAG, "onPushOpen");
    }

    @Override
    protected void onPushReceive(Context context, Intent intent) {
        Log.i(TAG, "onPushReceive");
        **super.onPushReceive(context, intent);**
    }
}