Skip to content

Commit

Permalink
Migrate to FCM and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Dobell committed Jul 20, 2017
1 parent 9dbc726 commit c967bff
Show file tree
Hide file tree
Showing 38 changed files with 691 additions and 599 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ android {

dependencies {
// Google's GCM.
compile 'com.google.android.gms:play-services-gcm:10.0.1'
compile "com.google.firebase:firebase-messaging:10.2.6"

compile 'com.facebook.react:react-native:+'

Expand Down
35 changes: 5 additions & 30 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wix.reactnativenotifications">

<!--
Permissions required for enabling GCM.
-->
<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />

<!-- Ref: http://stackoverflow.com/questions/13602190/java-lang-securityexception-requires-vibrate-permission-on-jelly-bean-4-2 -->
<uses-permission android:name="android.permission.VIBRATE" android:maxSdkVersion="18" />

Expand All @@ -21,39 +13,22 @@
-->
<service android:name=".core.ProxyService"/>

<!--
Google's ready-to-use GcmReceiver.
1. Awaits actual GCM messages (e.g. push notifications) and invokes the GCM service with the concrete content.
2. Awaits instance-ID/token refresh requests from the GCM and invokes the Instance-ID listener service.
-->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
<!-- Dispatched by the GcmReceiver when messages are received. -->
<service
android:name="com.wix.reactnativenotifications.gcm.GcmMessageHandlerService"
android:exported="false">
android:name="com.wix.reactnativenotifications.fcm.FcmMessageHandlerService">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<!-- Dispatched by the GcmReceiver. Starts the designated refresh-handling service. -->
<service
android:name=".gcm.GcmInstanceIdListenerService"
android:exported="false">
android:name=".fcm.FcmInstanceIdListenerService">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".gcm.GcmInstanceIdRefreshHandlerService"
android:name=".fcm.FcmTokenService"
android:exported="false" />
</application>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

public interface Defs {
String LOGTAG = "ReactNativeNotifs";
String GCM_SENDER_ID_ATTR_NAME = "com.wix.reactnativenotifications.gcmSenderId";

String TOKEN_RECEIVED_EVENT_NAME = "remoteNotificationsRegistered";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.facebook.react.bridge.Arguments;
Expand All @@ -17,12 +18,12 @@
import com.wix.reactnativenotifications.core.AppLifecycleFacadeHolder;
import com.wix.reactnativenotifications.core.InitialNotificationHolder;
import com.wix.reactnativenotifications.core.ReactAppLifecycleFacade;
import com.wix.reactnativenotifications.core.notification.IPushNotification;
import com.wix.reactnativenotifications.core.notification.PushNotification;
import com.wix.reactnativenotifications.core.notification.PushNotificationProps;
import com.wix.reactnativenotifications.core.notificationdrawer.IPushNotificationsDrawer;
import com.wix.reactnativenotifications.core.notificationdrawer.PushNotificationsDrawer;
import com.wix.reactnativenotifications.gcm.GcmInstanceIdRefreshHandlerService;
import com.wix.reactnativenotifications.core.notificationdrawer.INotificationDrawer;
import com.wix.reactnativenotifications.core.notificationdrawer.NotificationDrawer;
import com.wix.reactnativenotifications.core.notifications.ILocalNotification;
import com.wix.reactnativenotifications.core.notifications.LocalNotification;
import com.wix.reactnativenotifications.core.notifications.NotificationProps;
import com.wix.reactnativenotifications.fcm.FcmTokenService;

import static com.wix.reactnativenotifications.Defs.LOGTAG;

Expand All @@ -46,16 +47,14 @@ public String getName() {
@Override
public void initialize() {
Log.d(LOGTAG, "Native module init");
startGcmIntentService(GcmInstanceIdRefreshHandlerService.EXTRA_IS_APP_INIT);

final IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext());
final INotificationDrawer notificationsDrawer = NotificationDrawer.get(getReactApplicationContext().getApplicationContext());
notificationsDrawer.onAppInit();
}

@ReactMethod
public void refreshToken() {
Log.d(LOGTAG, "Native method invocation: refreshToken()");
startGcmIntentService(GcmInstanceIdRefreshHandlerService.EXTRA_MANUAL_REFRESH);
startTokenService(FcmTokenService.ACTION_REFRESH_TOKEN);
}

@ReactMethod
Expand All @@ -64,7 +63,7 @@ public void getInitialNotification(final Promise promise) {
Object result = null;

try {
final PushNotificationProps notification = InitialNotificationHolder.getInstance().get();
final NotificationProps notification = InitialNotificationHolder.getInstance().get();
if (notification == null) {
return;
}
Expand All @@ -76,22 +75,29 @@ public void getInitialNotification(final Promise promise) {
}

@ReactMethod
public void postLocalNotification(ReadableMap notificationPropsMap, int notificationId) {
public void postLocalNotification(ReadableMap propsMap, int notificationId) {
Log.d(LOGTAG, "Native method invocation: postLocalNotification");
final Bundle notificationProps = Arguments.toBundle(notificationPropsMap);
final IPushNotification pushNotification = PushNotification.get(getReactApplicationContext().getApplicationContext(), notificationProps);
pushNotification.onPostRequest(notificationId);
final Context context = getReactApplicationContext().getApplicationContext();
final NotificationProps localNotificationProps = NotificationProps.fromBundle(context, Arguments.toBundle(propsMap));
final ILocalNotification notification = LocalNotification.get(context, localNotificationProps);
notification.post(notificationId);
}

@ReactMethod
public void cancelLocalNotification(int notificationId, @Nullable String notificationTag) {
INotificationDrawer notificationsDrawer = NotificationDrawer.get(getReactApplicationContext().getApplicationContext());
notificationsDrawer.onCancelLocalNotification(notificationTag, notificationId);
}

@ReactMethod
public void cancelLocalNotification(int notificationId) {
IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext());
notificationsDrawer.onNotificationClearRequest(notificationId);
public void cancelAllLocalNotifications() {
INotificationDrawer notificationDrawer = NotificationDrawer.get(getReactApplicationContext().getApplicationContext());
notificationDrawer.onCancelAllLocalNotifications();
}

@Override
public void onAppVisible() {
final IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext());
final INotificationDrawer notificationsDrawer = NotificationDrawer.get(getReactApplicationContext().getApplicationContext());
notificationsDrawer.onAppVisible();
}

Expand All @@ -101,7 +107,7 @@ public void onAppNotVisible() {

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
final IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(getReactApplicationContext().getApplicationContext());
final INotificationDrawer notificationsDrawer = NotificationDrawer.get(getReactApplicationContext().getApplicationContext());
notificationsDrawer.onNewActivity(activity);
}

Expand Down Expand Up @@ -129,10 +135,10 @@ public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
public void onActivityDestroyed(Activity activity) {
}

protected void startGcmIntentService(String extraFlag) {
protected void startTokenService(String action) {
final Context appContext = getReactApplicationContext().getApplicationContext();
final Intent tokenFetchIntent = new Intent(appContext, GcmInstanceIdRefreshHandlerService.class);
tokenFetchIntent.putExtra(extraFlag, true);
appContext.startService(tokenFetchIntent);
final Intent intent = new Intent(appContext, FcmTokenService.class);
intent.setAction(action);
appContext.startService(intent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import android.support.annotation.Nullable;

import com.wix.reactnativenotifications.core.notification.PushNotificationProps;
import com.wix.reactnativenotifications.core.notifications.NotificationProps;

public class InitialNotificationHolder {

private static InitialNotificationHolder sInstance;

private PushNotificationProps mNotification;
private NotificationProps mNotification;

public static void setInstance(InitialNotificationHolder instance) {
sInstance = instance;
Expand All @@ -24,16 +24,16 @@ public static InitialNotificationHolder getInstance() {
return sInstance;
}

public void set(PushNotificationProps pushNotificationProps) {
mNotification = pushNotificationProps;
public void set(NotificationProps notificationProps) {
mNotification = notificationProps;
}

public void clear() {
mNotification = null;
}

@Nullable
public PushNotificationProps get() {
public NotificationProps get() {
return mNotification;
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
package com.wix.reactnativenotifications.core;

import android.content.Context;
import android.os.Bundle;

import com.facebook.react.ReactApplication;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

public class JsIOHelper {
public boolean sendEventToJS(String eventName, Bundle data, ReactContext reactContext) {
if (reactContext != null) {
sendEventToJS(eventName, Arguments.fromBundle(data), reactContext);
return true;
}
return false;

private final Context mContext;

public JsIOHelper(Context context) {
mContext = context;
}

public boolean sendEventToJS(String eventName, WritableMap data, ReactContext reactContext) {
if (reactContext != null) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, data);
public boolean sendEventToJS(String eventName, Bundle bundle) {
return sendEventToJS(eventName, Arguments.fromBundle(bundle));
}

public boolean sendEventToJS(String eventName, String string) {
return sendEventToJS(eventName, (Object) string);
}

boolean sendEventToJS(String eventName, Object object) {
final ReactContext reactContext = ((ReactApplication) mContext.getApplicationContext()).getReactNativeHost().getReactInstanceManager().getCurrentReactContext();

if (reactContext != null && reactContext.hasActiveCatalystInstance()) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, object);
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import android.content.Intent;
import android.os.Bundle;

import com.wix.reactnativenotifications.core.notification.PushNotificationProps;
import com.wix.reactnativenotifications.core.notifications.NotificationProps;

public class NotificationIntentAdapter {
private static final int PENDING_INTENT_CODE = 0;
private static final String PUSH_NOTIFICATION_EXTRA_NAME = "pushNotification";

public static PendingIntent createPendingNotificationIntent(Context appContext, Intent intent, PushNotificationProps notification) {
public static PendingIntent createPendingNotificationIntent(Context appContext, Intent intent, NotificationProps notification) {
intent.putExtra(PUSH_NOTIFICATION_EXTRA_NAME, notification.asBundle());
return PendingIntent.getService(appContext, PENDING_INTENT_CODE, intent, PendingIntent.FLAG_ONE_SHOT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import android.os.Bundle;
import android.util.Log;

import com.wix.reactnativenotifications.core.notification.IPushNotification;
import com.wix.reactnativenotifications.core.notification.PushNotification;
import com.wix.reactnativenotifications.core.notifications.ILocalNotification;
import com.wix.reactnativenotifications.core.notifications.LocalNotification;
import com.wix.reactnativenotifications.core.notifications.NotificationProps;

public class ProxyService extends IntentService {

Expand All @@ -19,10 +20,12 @@ public ProxyService() {
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "New intent: "+intent);
final Bundle notificationData = NotificationIntentAdapter.extractPendingNotificationDataFromIntent(intent);
final IPushNotification pushNotification = PushNotification.get(this, notificationData);
if (pushNotification != null) {
pushNotification.onOpened();
final Bundle notificationBundle = NotificationIntentAdapter.extractPendingNotificationDataFromIntent(intent);

if (notificationBundle != null) {
final NotificationProps notificationProps = NotificationProps.fromBundle(this, notificationBundle);
final ILocalNotification localNotification = LocalNotification.get(this, notificationProps);
localNotification.onOpened();
}
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit c967bff

Please sign in to comment.