当我最小化它时,通知会丢失

时间:2016-05-10 06:55:52

标签: android push-notification minimize

我遇到一个问题,当我在设备中收到通知时,我会打开它,当我最小化通知时,它会从最小化的应用列表中消失,因此我无法再看到我的通知。

这是messege处理程序服务

的代码
 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setNotification(String message,String bundledata)
    {
        NotificationManager  mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(this, GcmNotificationRedirect.class);
        notificationIntent.putExtra("pushMessage", message);
        notificationIntent.putExtra("bundleData",bundledata);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );

        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(this, NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


       // NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setContentTitle(title)
                        .setPriority(Notification.PRIORITY_MAX)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(message))
                        .setContentText(message)
                        .setNumber(pushNumber);

            mBuilder.setColor(this.getResources().getColor(R.color.theme_color));
            mBuilder.setSmallIcon(R.drawable.app_icon);
            mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), getNotificationIcon()));

        mBuilder.setContentIntent(resultPendingIntent);
        mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
        Notification notification = mBuilder.build();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        mNotificationManager.notify(NOTIFICATION_ID, notification);

    }

这是将数据传递给适当活动的活动。

//GcmNotificationRedirect
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pushRedirect();

    }

    private void pushRedirect()
    {
        pushMessage = getIntent().getStringExtra("pushMessage");
        bundleData = getIntent().getStringExtra("bundleData");
        parsePushNotifiactionJson(pushMessage,bundleData);
        finish();
    }

    private void parsePushNotifiactionJson(String pushMessage, String bundleJSONData){
        Intent pushNotiInetent = null;

        if(pushMessage!=null && bundleJSONData!=null) {
            try {

                JSONObject jsonObject = new JSONObject(bundleJSONData);

                if (jsonObject.getString("type").equals(PRODUCT_PAGE)) {

                    pojo = new Push_Notification_Pojo(jsonObject.getString("productId"), jsonObject.getString("type"),pushMessage);
                    pushNotiInetent = new Intent(this, ProductDetail.class);
                    pushNotiInetent.putExtra("push_notification_pojo", pojo);
                    startActivity(pushNotiInetent);
                }

                else
                {
                    pojo = new Push_Notification_Pojo(jsonObject.getString("promotionId"), jsonObject.getString("type"), pushMessage, true);

                    if(pojo!=null)
                    {
                        pushNotiInetent = new Intent(this,PushNotificationPromotion.class);
                        pushNotiInetent.putExtra("pushMessage", pushMessage);
                        pushNotiInetent.putExtra("bundleData",bundleData);
                        startActivity(pushNotiInetent);
                    }
                }
                Log.d("Push",pojo.getType()+" "+pojo.getProductId()+" "+pojo.getPromotionId() );

            }

            catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }


}

一切正常,但是当我最小化时,我无法收到通知。

1 个答案:

答案 0 :(得分:0)

您可能意味着何时解雇通知。这是预期的行为。如果您想要保留通知,意味着始终可见,您可以使用 setOngoing(true)

 NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setContentTitle(title)
                    .setPriority(Notification.PRIORITY_MAX)
                    .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(message))
                    .setOngoing(true)
                    .setContentText(message)
                    .setNumber(pushNumber);

要解雇它,请使用

 NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.cancel(NOTIFICATION_ID);

此致 弗拉基米尔