Search in sources :

Example 1 with RemoteMessage

use of com.google.firebase.messaging.RemoteMessage in project mobile-center-sdk-android by Microsoft.

the class PushTest method receivedInForeground.

@Test
public void receivedInForeground() {
    PushListener pushListener = mock(PushListener.class);
    Push.setListener(pushListener);
    Context contextMock = mock(Context.class);
    Push push = Push.getInstance();
    Channel channel = mock(Channel.class);
    push.onStarted(contextMock, DUMMY_APP_SECRET, channel);
    Activity activity = mock(Activity.class);
    when(activity.getIntent()).thenReturn(mock(Intent.class));
    push.onActivityResumed(activity);
    /* Mock some message. */
    RemoteMessage message = mock(RemoteMessage.class);
    RemoteMessage.Notification notification = mock(RemoteMessage.Notification.class);
    when(message.getNotification()).thenReturn(notification);
    when(notification.getTitle()).thenReturn("some title");
    when(notification.getBody()).thenReturn("some message");
    PushMessagingService service = new PushMessagingService();
    service.onMessageReceived(message);
    ArgumentCaptor<PushNotification> captor = ArgumentCaptor.forClass(PushNotification.class);
    verify(pushListener).onPushNotificationReceived(eq(activity), captor.capture());
    PushNotification pushNotification = captor.getValue();
    assertNotNull(pushNotification);
    assertEquals("some title", pushNotification.getTitle());
    assertEquals("some message", pushNotification.getMessage());
    assertEquals(new HashMap<String, String>(), pushNotification.getCustomData());
    /* If disabled, no notification anymore. */
    Push.setEnabled(false);
    service.onMessageReceived(message);
    /* Called once. */
    verify(pushListener).onPushNotificationReceived(eq(activity), captor.capture());
    /* Enabled but remove listener. */
    Push.setEnabled(true);
    Push.setListener(null);
    service.onMessageReceived(message);
    /* Called once. */
    verify(pushListener).onPushNotificationReceived(eq(activity), captor.capture());
    /* Mock null notification and custom data. */
    Push.setListener(pushListener);
    Map<String, String> data = new HashMap<>();
    data.put("a", "b");
    data.put("c", "d");
    when(message.getNotification()).thenReturn(null);
    when(message.getData()).thenReturn(data);
    service.onMessageReceived(message);
    verify(pushListener, times(2)).onPushNotificationReceived(eq(activity), captor.capture());
    pushNotification = captor.getValue();
    assertNotNull(pushNotification);
    assertNull(pushNotification.getTitle());
    assertNull(pushNotification.getMessage());
    assertEquals(data, pushNotification.getCustomData());
    /* Disable while posting the command to the U.I. thread. */
    activity = mock(Activity.class);
    when(activity.getIntent()).thenReturn(mock(Intent.class));
    push.onActivityResumed(activity);
    final AtomicReference<Runnable> runnable = new AtomicReference<>();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            runnable.set((Runnable) invocation.getArguments()[0]);
            return null;
        }
    }).when(HandlerUtils.class);
    HandlerUtils.runOnUiThread(any(Runnable.class));
    service.onMessageReceived(message);
    Push.setEnabled(false);
    runnable.get().run();
    verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
    /* Remove listener while posting to UI thread. */
    Push.setEnabled(true);
    service.onMessageReceived(message);
    Push.setListener(null);
    runnable.get().run();
    verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
    /* Update listener while posting to UI thread. */
    Push.setListener(pushListener);
    service.onMessageReceived(message);
    PushListener pushListener2 = mock(PushListener.class);
    Push.setListener(pushListener2);
    runnable.get().run();
    verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
    verify(pushListener2).onPushNotificationReceived(eq(activity), captor.capture());
}
Also used : Context(android.content.Context) RemoteMessage(com.google.firebase.messaging.RemoteMessage) HashMap(java.util.HashMap) Channel(com.microsoft.azure.mobile.channel.Channel) Activity(android.app.Activity) Intent(android.content.Intent) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with RemoteMessage

use of com.google.firebase.messaging.RemoteMessage in project react-native-fcm by evollu.

the class FIRMessagingModule method registerMessageHandler.

private void registerMessageHandler() {
    IntentFilter intentFilter = new IntentFilter("com.evollu.react.fcm.ReceiveNotification");
    LocalBroadcastManager.getInstance(getReactApplicationContext()).registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (getReactApplicationContext().hasActiveCatalystInstance()) {
                RemoteMessage message = intent.getParcelableExtra("data");
                WritableMap params = Arguments.createMap();
                WritableMap fcmData = Arguments.createMap();
                if (message.getNotification() != null) {
                    Notification notification = message.getNotification();
                    fcmData.putString("title", notification.getTitle());
                    fcmData.putString("body", notification.getBody());
                    fcmData.putString("color", notification.getColor());
                    fcmData.putString("icon", notification.getIcon());
                    fcmData.putString("tag", notification.getTag());
                    fcmData.putString("action", notification.getClickAction());
                }
                params.putMap("fcm", fcmData);
                params.putString("collapse_key", message.getCollapseKey());
                params.putString("from", message.getFrom());
                params.putString("google.message_id", message.getMessageId());
                params.putDouble("google.sent_time", message.getSentTime());
                if (message.getData() != null) {
                    Map<String, String> data = message.getData();
                    Set<String> keysIterator = data.keySet();
                    for (String key : keysIterator) {
                        params.putString(key, data.get(key));
                    }
                }
                sendEvent("FCMNotificationReceived", params);
            }
        }
    }, intentFilter);
}
Also used : Context(android.content.Context) ReactApplicationContext(com.facebook.react.bridge.ReactApplicationContext) IntentFilter(android.content.IntentFilter) RemoteMessage(com.google.firebase.messaging.RemoteMessage) WritableMap(com.facebook.react.bridge.WritableMap) Set(java.util.Set) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) Map(java.util.Map) ReadableMap(com.facebook.react.bridge.ReadableMap) WritableMap(com.facebook.react.bridge.WritableMap) Notification(com.google.firebase.messaging.RemoteMessage.Notification)

Aggregations

Context (android.content.Context)2 Intent (android.content.Intent)2 RemoteMessage (com.google.firebase.messaging.RemoteMessage)2 Activity (android.app.Activity)1 BroadcastReceiver (android.content.BroadcastReceiver)1 IntentFilter (android.content.IntentFilter)1 ReactApplicationContext (com.facebook.react.bridge.ReactApplicationContext)1 ReadableMap (com.facebook.react.bridge.ReadableMap)1 WritableMap (com.facebook.react.bridge.WritableMap)1 Notification (com.google.firebase.messaging.RemoteMessage.Notification)1 Channel (com.microsoft.azure.mobile.channel.Channel)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)1