Notification kill app然后创建新实例

时间:2015-04-24 11:27:10

标签: android android-notifications

我有2个按钮的通知。 当用户点击通知的任何按钮时,它应该:
1.恢复应用程序。
2.回复点击通知按钮。

现在应用程序响应点击按钮,但每次应用程序被杀死时都会创建它的新实例,这是我不想要的。 我认为旗帜是错误的。我尝试过类似于我的问题的其他堆栈解决方案的标志,但是我没有从按钮接收意图,所以我无法识别单击了哪个按钮。

我如何在片段的onResume()中接收按钮意图:

mNotificationManager =
            (NotificationManager) getActivity().getSystemService(getActivity().getApplicationContext().NOTIFICATION_SERVICE);

    String extras = "";

    if (getActivity().getIntent().getExtras() != null) {
        Log.i(TAG, "[NOTIFICATION] Extras not null");
        extras = (String) getActivity().getIntent().getExtras().get("DO");
    }

    if (extras != null && extras != "") {
        Log.i(TAG, "[NOTIFICATION] Received notification bundle.");

        String btnClicked = extras;

        Log.i(TAG, "[NOTIFICATION BUTTON]" + btnClicked);

        if (btnClicked.equals(SMS_PAYMENT)) {
            Log.i(TAG, "[NOTIFICATION] SMS payment button cliked");
            createSmsPaymentDialog();
        }
        if (btnClicked.equals(SET_TIMER)) {
            Log.i(TAG, "[NOTIFICATION] Set timer button cliked");
            showTimer();
        }

        mNotificationManager.cancel(NOTIFICATION_ID);

我如何创建通知:

  private void generateNotification() {

    Log.i(TAG, "Notification should arrive now.");

    RemoteViews contentView = new RemoteViews(getActivity().getPackageName(), R.layout.notification_layout);

    Notification noti = new Notification.Builder(getActivity().getApplicationContext())
            .setSmallIcon(R.drawable.scp_180)
            .build();

    noti.bigContentView = setNotificationListenersAndContent(contentView);

    noti.flags =  Notification.FLAG_AUTO_CANCEL;

    noti.defaults |= Notification.DEFAULT_LIGHTS; // LED
    //noti.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
   // noti.defaults |= Notification.DEFAULT_SOUND; // Sound

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

    notificationManager.notify(NOTIFICATION_ID, noti);
}

public RemoteViews setNotificationListenersAndContent(RemoteViews view) {

    //setting Buttons
    Intent iSendSMS = new Intent(getActivity(), MainActivity.class);
    iSendSMS.putExtra("DO", "btnSendSms");
    iSendSMS.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    iSendSMS.setAction("btnSendSms");
    PendingIntent pApp = PendingIntent.getActivity(getActivity(), 0, iSendSMS, PendingIntent.FLAG_UPDATE_CURRENT); // byl ONE_SHOT
    view.setOnClickPendingIntent(R.id.btnSendSms, pApp);

    Intent iSetTimer = new Intent(getActivity(), MainActivity.class);
    iSetTimer.putExtra("DO", "btnSetTimer");
    iSetTimer.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    iSetTimer.setAction("btnSetTimer");
    PendingIntent pApp2 = PendingIntent.getActivity(getActivity(), 0, iSetTimer,PendingIntent.FLAG_UPDATE_CURRENT);
    view.setOnClickPendingIntent(R.id.btnSetTimer, pApp2);

    //setting image
    view.setImageViewResource(R.id.notificationImage, R.drawable.scp_180);
    view.setTextViewText(R.id.notificationTitle, "some title");
    view.setTextViewText(R.id.notificationDescriptionText, "some content");

    return view;
}

编辑,添加了logcat日志:

I/ScpFragment﹕ Notification should arrive now.  
I/ScpFragment﹕ OnPause
I/ScpFragment﹕ App in background: true
I/MainActivity﹕ onPause
I/MainActivity﹕ isFinishing = true, cleaning app.
I/MainActivity﹕ OnCreate..
I/MainActivity﹕ connecting to googleApi....
I/MainActivity﹕ onStart
I/MainActivity﹕ onResume
I/MainActivity﹕ GoogleApiClient connected
I/ScpFragment﹕ OnCreate..
I/ScpFragment﹕ OnCreateView
I/ScpFragment﹕ Setting Controlls and Listeners.
I/ScpFragment﹕ onResume..
I/ScpFragment﹕ App in background: false
I/ScpFragment﹕ [NOTIFICATION] Extras not null.
I/ScpFragment﹕ [NOTIFICATION] Reveived notification bundle.
I/ScpFragment﹕ [NOTIFICATION BUTTON]btnSetTimer
I/ScpFragment﹕ [NOTIFICATION] Set timer button cliked

0 个答案:

没有答案