2017-02-20 14 views
0

Я получаю уведомления от пользователя с помощью кнопок actionButtons, отправленных из службы. Как я могу справиться с этими кнопками и обрабатывать их события?Как работать с существующим действием Кнопки

protected boolean onNotificationProcessing(final OSNotificationReceivedResult receivedResult) { 
      OverrideSettings overrideSettings = new OverrideSettings(); 

      // here I am getting the data sent thtough oneSignal 
      final JSONObject additionalData = receivedResult.payload.additionalData; 

      // here I am getting my buttons 
      final List<OSNotificationPayload.ActionButton> actionButtons = receivedResult.payload.actionButtons; 

      overrideSettings.extender = new NotificationCompat.Extender() { 
      @Override 
      public NotificationCompat.Builder extend(NotificationCompat.Builder builder) { 
      try { 

      // Here I am creating my own button and adding it to the notification 

      Intent intent = new Intent(Intent.ACTION_DIAL); 
      intent.setData(Uri.parse("tel:" + phoneNumber)); 
      PendingIntent pendingIntentCall = PendingIntent.getActivity(MyService.this, 12345, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
      builder.addAction(R.drawable.phone_notif,getString(R.string.call),pendingIntentCall); 
      } catch (Exception e) { 
      } 
      return builder; 
     } 
     }; 
    return true 
    } 

Как я могу обрабатывать событие для действий, которые принимаются от одного сигнала? Они появляются, но когда я нажимаю на них, они действуют, как будто я нажимаю на уведомление ...

ответ

0

Android SDK OneSignal предоставляет обработчики полученных уведомлений. Вы должны установить идентификаторы для своих кнопок, чтобы отличать один от другого. Вы также можете отправить дополнительные данные (например, URL-адрес для посещения или некоторые другие метаданные) в качестве хэша под ключом data. От их documentation:

class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler { 
    // This fires when a notification is opened by tapping on it. 
    @Override 
    public void notificationOpened(OSNotificationOpenResult result) { 
    OSNotificationAction.ActionType actionType = result.action.type; 
    JSONObject data = result.notification.payload.additionalData; 
    String customKey; 

    if (data != null) { 
     customKey = data.optString("customkey", null); 
     if (customKey != null) 
     Log.i("OneSignalExample", "customkey set with value: " + customKey); 
    } 

    if (actionType == OSNotificationAction.ActionType.ActionTaken) 
     Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID); 

    // The following can be used to open an Activity of your choice. 
    // Replace - getApplicationContext() - with any Android Context. 
    // Intent intent = new Intent(getApplicationContext(), YourActivity.class); 
    // intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); 
    // startActivity(intent); 

    // Add the following to your AndroidManifest.xml to prevent the launching of your main Activity 
    // if you are calling startActivity above. 
    /* 
     <application ...> 
      <meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" /> 
     </application> 
    /* 
    } 
}