繁体   English   中英

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

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

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

我的扩展Application类在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方法,默​​认行为将保留(在状态栏中显示通知。之所以会出现此行为,是因为如果应用程序正在运行Parse Push通知,则将调用不执行任何操作的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);**
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM