Search in sources :

Example 1 with NotificationAction

use of org.infobip.mobile.messaging.interactive.NotificationAction in project mobile-messaging-sdk-android by infobip.

the class NotificationActionTapReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    Bundle actionBundle = intent.getBundleExtra(EXTRA_TAPPED_ACTION);
    Bundle categoryBundle = intent.getBundleExtra(EXTRA_TAPPED_CATEGORY);
    int notificationId = intent.getIntExtra(BroadcastParameter.EXTRA_NOTIFICATION_ID, -1);
    Bundle messageBundle = intent.getBundleExtra(BroadcastParameter.EXTRA_MESSAGE);
    Message message = Message.createFrom(messageBundle);
    NotificationCategory notificationCategory = NotificationCategory.createFrom(categoryBundle);
    NotificationAction notificationAction = NotificationAction.createFrom(actionBundle);
    String inputText = getInputTextFromIntent(intent, notificationAction);
    cancelNotification(context, notificationId);
    if (message == null) {
        MobileMessagingLogger.e("Received no message in NotificationActionTapReceiver");
        return;
    }
    if (notificationAction == null) {
        MobileMessagingLogger.e("Received no action in NotificationActionTapReceiver");
        return;
    }
    if (notificationCategory == null) {
        MobileMessagingLogger.e("Received no notification category in NotificationActionTapReceiver");
        return;
    }
    if (inputText != null) {
        notificationAction.setInputText(inputText);
    }
    broadcaster(context).notificationActionTapped(message, notificationCategory, notificationAction);
    mobileInteractive(context).triggerSdkActionsFor(notificationCategory.getCategoryId(), notificationAction, message);
    startCallbackActivity(context, intent, messageBundle, actionBundle, categoryBundle);
}
Also used : Message(org.infobip.mobile.messaging.Message) NotificationAction(org.infobip.mobile.messaging.interactive.NotificationAction) Bundle(android.os.Bundle) NotificationCategory(org.infobip.mobile.messaging.interactive.NotificationCategory)

Example 2 with NotificationAction

use of org.infobip.mobile.messaging.interactive.NotificationAction in project mobile-messaging-sdk-android by infobip.

the class NotificationActionTapReceiver method startCallbackActivity.

private void startCallbackActivity(Context context, Intent intent, Bundle messageBundle, Bundle actionBundle, Bundle categoryBundle) {
    NotificationAction notificationAction = NotificationActionBundleMapper.notificationActionFromBundle(actionBundle);
    if (notificationAction == null) {
        return;
    }
    if (!notificationAction.bringsAppToForeground()) {
        return;
    }
    NotificationSettings notificationSettings = mobileMessagingCore(context).getNotificationSettings();
    if (notificationSettings == null) {
        return;
    }
    Class callbackActivity = notificationSettings.getCallbackActivity();
    if (callbackActivity == null) {
        MobileMessagingLogger.e("Callback activity is not set, cannot proceed");
        return;
    }
    int intentFlags = intent.getIntExtra(MobileMessagingProperty.EXTRA_INTENT_FLAGS.getKey(), (Integer) MobileMessagingProperty.INTENT_FLAGS.getDefaultValue());
    Intent callbackIntent = new Intent(context, callbackActivity);
    callbackIntent.setAction(InteractiveEvent.NOTIFICATION_ACTION_TAPPED.getKey());
    callbackIntent.putExtras(messageBundle);
    callbackIntent.putExtras(actionBundle);
    callbackIntent.putExtras(categoryBundle);
    // FLAG_ACTIVITY_NEW_TASK has to be here because we're starting activity outside of activity context
    callbackIntent.addFlags(intentFlags | Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(callbackIntent);
}
Also used : NotificationAction(org.infobip.mobile.messaging.interactive.NotificationAction) NotificationSettings(org.infobip.mobile.messaging.NotificationSettings) Intent(android.content.Intent)

Example 3 with NotificationAction

use of org.infobip.mobile.messaging.interactive.NotificationAction in project mobile-messaging-sdk-android by infobip.

the class NotificationActionTapReceiverTest method test_should_send_notification_action_clicked_event.

@Test
public void test_should_send_notification_action_clicked_event() throws Exception {
    // Given
    Message givenMessage = createMessage(context, "SomeMessageId", false);
    int givenNotificationId = 1234;
    NotificationAction givenTappedNotificationAction = givenNotificationAction("actionId").build();
    NotificationCategory givenNotificationCategory = givenNotificationCategory(givenTappedNotificationAction);
    Intent givenIntent = givenIntent(givenMessage, givenNotificationCategory, givenTappedNotificationAction, givenNotificationId, notificationSettings.getIntentFlags());
    // When
    notificationActionTapReceiver.onReceive(contextMock, givenIntent);
    // Then
    Mockito.verify(notificationManagerMock, Mockito.times(1)).cancel(givenNotificationId);
    Mockito.verify(broadcastSender, Mockito.times(1)).notificationActionTapped(messageArgumentCaptor.capture(), notificationCategoryArgumentCaptor.capture(), notificationActionArgumentCaptor.capture());
    NotificationAction actualAction = notificationActionArgumentCaptor.getValue();
    NotificationCategory actualCategory = notificationCategoryArgumentCaptor.getValue();
    Message actualMessage = messageArgumentCaptor.getValue();
    assertJEquals(givenTappedNotificationAction, actualAction);
    assertJEquals(givenNotificationCategory, actualCategory);
    assertJEquals(givenMessage, actualMessage);
    Mockito.verify(mobileInteractive, Mockito.times(1)).triggerSdkActionsFor(actualCategory.getCategoryId(), actualAction, actualMessage);
    Mockito.verify(contextMock, Mockito.never()).startActivity(any(Intent.class));
}
Also used : Message(org.infobip.mobile.messaging.Message) NotificationAction(org.infobip.mobile.messaging.interactive.NotificationAction) Intent(android.content.Intent) NotificationCategory(org.infobip.mobile.messaging.interactive.NotificationCategory) Test(org.junit.Test)

Example 4 with NotificationAction

use of org.infobip.mobile.messaging.interactive.NotificationAction in project mobile-messaging-sdk-android by infobip.

the class NotificationActionTapReceiverTest method test_should_send_action_clicked_event_and_open_activity.

@Test
public void test_should_send_action_clicked_event_and_open_activity() throws Exception {
    // Given
    Message givenMessage = createMessage(context, "SomeMessageId", false);
    int givenNotificationId = 1234;
    NotificationAction givenTappedNotificationAction = givenNotificationAction("actionId").withBringingAppToForeground(true).build();
    NotificationCategory givenNotificationCategory = givenNotificationCategory(givenTappedNotificationAction);
    Intent givenIntent = givenIntent(givenMessage, givenNotificationCategory, givenTappedNotificationAction, givenNotificationId, notificationSettings.getIntentFlags());
    // When
    notificationActionTapReceiver.onReceive(contextMock, givenIntent);
    // Then
    Mockito.verify(notificationManagerMock, Mockito.times(1)).cancel(givenNotificationId);
    Mockito.verify(broadcastSender, Mockito.times(1)).notificationActionTapped(messageArgumentCaptor.capture(), notificationCategoryArgumentCaptor.capture(), notificationActionArgumentCaptor.capture());
    Message actualMessage = messageArgumentCaptor.getValue();
    NotificationAction actualAction = notificationActionArgumentCaptor.getValue();
    NotificationCategory actualCategory = notificationCategoryArgumentCaptor.getValue();
    assertJEquals(givenNotificationCategory, actualCategory);
    assertJEquals(givenMessage, actualMessage);
    assertJEquals(givenTappedNotificationAction, actualAction);
    Mockito.verify(mobileInteractive, Mockito.times(1)).triggerSdkActionsFor(actualCategory.getCategoryId(), actualAction, actualMessage);
    Mockito.verify(contextMock, Mockito.times(1)).startActivity(intentArgumentCaptor.capture());
    Intent actualIntent = intentArgumentCaptor.getValue();
    NotificationAction actualTappedAction = NotificationAction.createFrom(actualIntent.getExtras());
    NotificationCategory actualTappedCategory = NotificationCategory.createFrom(actualIntent.getExtras());
    Message actualTappedMessage = Message.createFrom(actualIntent.getExtras());
    assertEquals(givenIntent.getAction(), actualIntent.getAction());
    assertEquals(notificationSettings.getIntentFlags() | Intent.FLAG_ACTIVITY_NEW_TASK, actualIntent.getFlags());
    assertJEquals(givenTappedNotificationAction, actualTappedAction);
    assertJEquals(givenNotificationCategory, actualTappedCategory);
    assertJEquals(givenMessage, actualTappedMessage);
}
Also used : Message(org.infobip.mobile.messaging.Message) NotificationAction(org.infobip.mobile.messaging.interactive.NotificationAction) Intent(android.content.Intent) NotificationCategory(org.infobip.mobile.messaging.interactive.NotificationCategory) Test(org.junit.Test)

Example 5 with NotificationAction

use of org.infobip.mobile.messaging.interactive.NotificationAction in project mobile-messaging-sdk-android by infobip.

the class MainActivity method onNotificationActionTapped.

private void onNotificationActionTapped(Intent intent) {
    NotificationAction action = NotificationAction.createFrom(intent.getExtras());
    if (action == null)
        return;
    String actionTappedText = String.format(Locale.getDefault(), getString(R.string.toast_notification_action_tapped), action.getId());
    Toast.makeText(MainActivity.this, actionTappedText, Toast.LENGTH_LONG).show();
    updateCount();
}
Also used : NotificationAction(org.infobip.mobile.messaging.interactive.NotificationAction)

Aggregations

NotificationAction (org.infobip.mobile.messaging.interactive.NotificationAction)8 Intent (android.content.Intent)5 Message (org.infobip.mobile.messaging.Message)5 NotificationCategory (org.infobip.mobile.messaging.interactive.NotificationCategory)5 Test (org.junit.Test)4 PendingIntent (android.app.PendingIntent)1 Bundle (android.os.Bundle)1 NotificationSettings (org.infobip.mobile.messaging.NotificationSettings)1