0

私は3つの異なるアクションで通知しています。すなわちコール、SMS、スヌーズ。しかし、通知が来るたびに、何かアクションをクリックすると、メインアクティビティのみが開きますが、アクションは実行されません。アプリが開いていると、アクションが実行されます。通知アクションはアクションを実行するために2回クリックする必要があります

If App Open:アクションが実行され、通知が拒否されます。
アプリが開いていない場合:アプリが開かれ、何も実行されず、通知がそこに残ります。ここで

は私ReminderService.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.java

@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); 
       } 
      } 
     } 
    } 
} 

ですだから私はアクションを実行するためのアプリと第二を開くために、アクションを実行するために二回ワンクリックをクリックする必要があります。

私が間違っていることを教えてください。

答えて

1

アプリはNotificationをクリックすると、これはMainActivity()の新しいインスタンスを起動し、IntentonCreate()を呼び出し、それがますないコールonNewIntent()ます実行されていない場合。したがって、onCreate()の場合は、NotificationIntentの「補足」または「動作」を参照)をクリックしてアプリを起動したかどうかを確認する必要があります。executeNotification()onCreate()に電話する必要があります。

関連する問題