Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 通知操作需要单击两次以执行操作_Android_Android Notifications_Android Pendingintent_Notification Action - Fatal编程技术网

Android 通知操作需要单击两次以执行操作

Android 通知操作需要单击两次以执行操作,android,android-notifications,android-pendingintent,notification-action,Android,Android Notifications,Android Pendingintent,Notification Action,我有三个不同动作的通知。i、 打电话、发短信、打瞌睡。但每当收到通知时,我单击任何操作,然后只打开主活动,而不执行任何操作。但若应用程序是打开的,那个么动作就会被执行 如果应用程序打开:操作将被执行并通知解除。 如果应用程序未打开:应用程序将打开,不会执行任何操作,通知将留在那里 这是我的提醒服务.java Intent intentCall = new Intent(this, MainActivity.class); intentCall.setAction(Constants.NO

我有三个不同动作的通知。i、 打电话、发短信、打瞌睡。但每当收到通知时,我单击任何操作,然后只打开主活动,而不执行任何操作。但若应用程序是打开的,那个么动作就会被执行

如果应用程序打开:操作将被执行并通知解除。
如果应用程序未打开:应用程序将打开,不会执行任何操作,通知将留在那里

这是我的提醒服务.java

Intent intentCall = new Intent(this, MainActivity.class);
    intentCall.setAction(Constants.NOTIFY_CALL);
    intentCall.putExtra("rowId", rowId);
    intentCall.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntentCall = PendingIntent.getActivity(this, (int)rowId, intentCall, PendingIntent.FLAG_UPDATE_CURRENT);

    //Maybe intent
    Intent intentSMS = new Intent(this, MainActivity.class);
    intentSMS.setAction(Constants.NOTIFY_SMS);
    intentSMS.putExtra("rowId", rowId);
    intentSMS.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntentSms = PendingIntent.getActivity(this, (int)rowId, intentSMS, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent snoozeIntent = new Intent(this, MainActivity.class);
    snoozeIntent.setAction(Constants.NOTIFY_SNOOZE);
    snoozeIntent.putExtra("rowId", rowId);
    snoozeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntentSnooze = PendingIntent.getActivity(this, (int)rowId, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    //Initialize NotificationManager using Context.NOTIFICATION_SERVICE
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    //Prepare Notification Builder
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);


    if(notes.length() <= 0){
        notificationBuilder.setContentText("Do you want to call or sms to "+name);
        notificationBuilder.setStyle(new NotificationCompat.BigTextStyle()
                .bigText("Do you want to call or sms to "+name));

    } else {
        notificationBuilder.setContentText(notes);
        notificationBuilder.setStyle(new NotificationCompat.BigTextStyle()
                .bigText(notes));
    }

    notificationBuilder.setContentTitle(name);
    notificationBuilder.setSmallIcon(getNotificationIcon());
    notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    notificationBuilder.addAction(R.mipmap.ic_stat_call, "", pendingIntentCall);
    notificationBuilder.addAction(R.mipmap.ic_stat_sms, "", pendingIntentSms);
    notificationBuilder.addAction(R.mipmap.ic_stat_snooze, "", pendingIntentSnooze);
    notificationBuilder.setAutoCancel(true);        

    notificationManager.notify((int)rowId, notificationBuilder.build());  
所以我需要点击两次来执行操作,一次点击打开应用程序,第二次点击执行操作


请让我知道我做错了什么

如果当您单击
通知时应用程序未运行,这将启动
MainActivity()
的新实例,并使用
意图调用
onCreate()
,但不会调用
onNewIntent()
。因此,在
onCreate()
中,您需要检查应用程序是否由于单击
通知而启动(检查
意图中的“附加”或“操作”),如果是,您需要在
onCreate()中调用
executeNotification()

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    executeNotification(intent);
}

private void executeNotification(Intent intent) {

    LogFile.appendLog(" In Execute Notification : " + intent.getExtras().getLong("rowId"));
    long rowId;
    if (intent.getExtras() != null) {
        if (intent.getExtras().getLong("rowId") > 0) {
            LogFile.appendLog("Row Id received : -" + intent.getExtras().getLong("rowId"));
            rowId = intent.getExtras().getLong("rowId");
            NotificationManager mNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.cancel((int) rowId);
            RemindersDbAdapter mDBHelper = new RemindersDbAdapter(this);
            mDBHelper.open();
            Cursor cursor = mDBHelper.fetchReminder(rowId);
            String Number = cursor.getString(cursor.getColumnIndex(RemindersDbAdapter.KEY_NUMBER));
            if (intent.getAction() != null) {
                if (intent.getAction().equalsIgnoreCase(Constants.NOTIFY_CALL)) {
                    LogFile.appendLog("executeNotification() : received notification Call time:" + " rowId : " + rowId);

                    makeReminderCall(Number);
                } else if (intent.getAction().equalsIgnoreCase(Constants.NOTIFY_SMS)) {
                    LogFile.appendLog("executeNotification() : received notification SMS :" + " rowId : " + rowId);
                    sendReminderSMS(Number);
                } else if (intent.getAction().equalsIgnoreCase(Constants.NOTIFY_SNOOZE)) {
                    LogFile.appendLog("executeNotification() : received notification SNOOZE :" + " rowId : " + rowId);
                    snoozeReminder((int) rowId);
                }
            }
        }
    }
}