diff --git a/README.md b/README.md index 962278da9db0ef54cbd9c993376e14f1369cd026..078c9501302d310aa6148f594e2e11d29a7d11a4 100644 --- a/README.md +++ b/README.md @@ -76,8 +76,8 @@ And the following methods to support registration and receiving notifications: Add a reference to the library's native code in your global `settings.gradle`: ``` -include ':reactnativenotifications' -project(':reactnativenotifications').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-notifications/android') +include ':react-native-notifications' +project(':react-native-notifications').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-notifications/android') ``` Declare the library as a dependency in your **app-project's** `build.gradle`: @@ -85,7 +85,8 @@ Declare the library as a dependency in your **app-project's** `build.gradle`: ``` dependencies { // ... - compile project(':reactnativenotifications') + + compile project(':react-native-notifications') } ``` @@ -95,13 +96,13 @@ dependencies { Push notifications on Android are managed and dispatched using [Google's GCM service](https://developers.google.com/cloud-messaging/gcm) (now integrated into Firebase). The following installation steps are a TL;DR of [Google's GCM setup guide](https://developers.google.com/cloud-messaging/android/client). You can follow them to get GCM integrated quickly, but we recommend that you will in the very least have a peek at the guide's overview. -##### Step #1: Subscribe to Google's GCM +#### Step #1: Subscribe to Google's GCM To set GCM in your app, you must first create a Google API-project and obtain a **Sender ID** and a **Server API Key**. If you have no existing API project yet, the easiest way to go about in creating one is using [this step-by-step installation process](https://developers.google.com/mobile/add); Use [this tutorial](https://code.tutsplus.com/tutorials/how-to-get-started-with-push-notifications-on-android--cms-25870) for insturctions. Alternatively, follow [Google's complete guide](https://developers.google.com/cloud-messaging/android/client#create-an-api-project). -##### Step #2: Add Sender ID to Manifest File +#### Step #2: Add Sender ID to Manifest File Once obtained, bundle the Sender ID onto your main `manifest.xml` file: @@ -118,6 +119,15 @@ Once obtained, bundle the Sender ID onto your main `manifest.xml` file: ``` +#### Step #3: Add / verify GCM dependency in `build.gradle`: + +``` +dependencies { + // If you haven't already done so, add Google's GCM module: + compile "com.google.android.gms:play-services-gcm:9+" +} +``` + --- diff --git a/android/build.gradle b/android/build.gradle index 97f3bf2a52ba8e5bd84e43d9133013aca5a48675..236cfe64a5c4f97edb953ae7c56295f8b455ac19 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -20,7 +20,7 @@ android { dependencies { // Google's GCM related framework components. - compile "com.google.android.gms:play-services-gcm:9+" + compile "com.google.android.gms:play-services-gcm:9.4.0" compile 'com.facebook.react:react-native:+' testCompile 'junit:junit:4.12' diff --git a/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java b/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java index 9d8fd28434cdd130bf342827db3988dbf40c20f7..c47efe14c0300cfd6ce8e785387c94c2c4beb556 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java +++ b/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java @@ -44,7 +44,7 @@ public class PushNotification implements IPushNotification { protected PushNotification(Context context, Bundle bundle, AppLifecycleFacade appLifecycleFacade) { mContext = context; mAppLifecycleFacade = appLifecycleFacade; - mNotificationProps = new PushNotificationProps(bundle); + mNotificationProps = createProps(bundle); } public static IPushNotification get(Context context, Bundle bundle, AppLifecycleFacade facade) { @@ -97,6 +97,10 @@ public class PushNotification implements IPushNotification { } } + protected PushNotificationProps createProps(Bundle bundle) { + return new PushNotificationProps(bundle); + } + protected void setAsInitialNotification() { InitialNotification.set(mNotificationProps); } diff --git a/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotificationProps.java b/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotificationProps.java index d4512ced5ab6a874ddbcd0312aee5fb8209b5819..b155dfa030021809938eb3bb89c6d22d908f71cf 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotificationProps.java +++ b/android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotificationProps.java @@ -2,20 +2,21 @@ package com.wix.reactnativenotifications.core.notification; import android.os.Bundle; -/** - * @author amitd - */ public class PushNotificationProps { - private Bundle mBundle; + protected Bundle mBundle; - public PushNotificationProps(Bundle bundle) { - final String title = bundle.getString("title"); - final String body = bundle.getString("body"); - if (title == null || title.trim().isEmpty() || body == null || body.trim().isEmpty()) { - throw new IllegalArgumentException("Invalid notification"); - } + public PushNotificationProps() { + mBundle = new Bundle(); + } + public PushNotificationProps(String title, String body) { + mBundle = new Bundle(); + mBundle.putString("title", title); + mBundle.putString("body", body); + } + + public PushNotificationProps(Bundle bundle) { mBundle = bundle; } diff --git a/android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/INotificationsDrawerApplication.java b/android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/INotificationsDrawerApplication.java index fe17c74c232aa729462c7b35b9a8a5ee4127a124..c11c32dc595b05a6793a41fb5ea132328b1194dc 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/INotificationsDrawerApplication.java +++ b/android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/INotificationsDrawerApplication.java @@ -1,5 +1,7 @@ package com.wix.reactnativenotifications.core.notificationdrawer; +import android.content.Context; + public interface INotificationsDrawerApplication { - IPushNotificationsDrawer getPushNotificationsDrawer(); + IPushNotificationsDrawer getPushNotificationsDrawer(Context context); } diff --git a/android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/PushNotificationsDrawer.java b/android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/PushNotificationsDrawer.java index 7623f8fa0bd96aa22f4086fe36268ed79608afc5..9b3383bdcc7af65cf63bc4c3159806fd813daed4 100644 --- a/android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/PushNotificationsDrawer.java +++ b/android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/PushNotificationsDrawer.java @@ -18,7 +18,7 @@ public class PushNotificationsDrawer implements IPushNotificationsDrawer { public static IPushNotificationsDrawer get(Context context) { final Context appContext = context.getApplicationContext(); if (appContext instanceof INotificationsDrawerApplication) { - return ((INotificationsDrawerApplication) appContext).getPushNotificationsDrawer(); + return ((INotificationsDrawerApplication) appContext).getPushNotificationsDrawer(context); } return new PushNotificationsDrawer(context); diff --git a/example/android/myapplication/build.gradle b/example/android/myapplication/build.gradle index 00e378ed0a1021b50bef8f44025ed7db8a190a4e..f97a9e84157791dff72a4c9d1300cd6d15b8c025 100644 --- a/example/android/myapplication/build.gradle +++ b/example/android/myapplication/build.gradle @@ -32,7 +32,8 @@ dependencies { compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support:design:23.4.0' compile 'com.facebook.react:react-native:+' - compile project(':reactnativenotifications') + compile "com.google.android.gms:play-services-gcm:9+" + compile project(':react-native-notifications') testCompile 'junit:junit:4.12' } diff --git a/example/android/settings.gradle b/example/android/settings.gradle index ac703f3bf6a6dee757b62a5747d3115d1b3eb2bf..9ac95524a634aeec524ee31b3c6a27c7f59fa1f9 100644 --- a/example/android/settings.gradle +++ b/example/android/settings.gradle @@ -1,4 +1,4 @@ include ':myapplication' -include ':reactnativenotifications' -project(':reactnativenotifications').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-notifications/android') +include ':react-native-notifications' +project(':react-native-notifications').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-notifications/android') diff --git a/index.android.js b/index.android.js index e0517bc8b8a7d328a4423e2ff6cd3e3d94982ff3..36d429257e6a6963fe657e80df51592a3f63d913 100644 --- a/index.android.js +++ b/index.android.js @@ -41,6 +41,10 @@ export class NotificationsAndroid { notificationReceivedListener = null; } } + + static refreshToken() { + RNNotifications.refreshToken(); + } } export class PendingNotifications {