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());
}
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);
}
Aggregations