NotificationCompat.Builder显示对话框而不是通知

时间:2019-07-09 07:40:14

标签: android push-notification onesignal

我正在聊天应用程序中,通过OneSignal使用推送通知。

如果用户在应用程序中,但不在对话活动中,则当收到通知时,将绘制一个对话框,而不是通知。

我想显示一个通知,而不是一个对话框。这是代码:

protected boolean onNotificationProcessing(OSNotificationReceivedResult notification) {
    almacen=((Almacen)getApplication());
    JSONObject noti=notification.payload.additionalData;
    try {
        idChat=noti.getInt("idchat");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    showNoti=!(almacen.getIdChat()==idChat);
    if(showNoti){
        OverrideSettings overrideSettings = new OverrideSettings();
        overrideSettings.extender = new NotificationCompat.Extender() {
            @Override
            public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
                Bitmap icon = BitmapFactory.decodeResource(Almacen.getContext().getResources(),
                        R.drawable.chat);
                builder.setLargeIcon(icon);
                return builder.setColor(new BigInteger("FF0000FF", 16).intValue());
            }
        };

        OSNotificationDisplayedResult displayedResult = displayNotification(overrideSettings);
        Log.d("OneSignalExample", "Notification displayed with id: " + displayedResult.androidNotificationId);
    }else{
        if(almacen.getActivity()!=null){
            almacen.getActivity().reload();
        }
    }


    return true;
}

使用此应用,应用程序的行为类似于说的:显示一个对话框,而不是通知。

如何避免这种情况,而是显示通知而不是“对话框”?

谢谢。

1 个答案:

答案 0 :(得分:0)

我已经解决了,方法是添加以下行:

.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)

在startInit方法中:

OneSignal.startInit(this)
            .setNotificationOpenedHandler(new MyNotificationOpenedHandler())
            .setNotificationReceivedHandler(new MyNotificationReceivedHandler())
            .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
            .init();
相关问题