From bac11ebc547529f7c06d482b02c801480a01b266 Mon Sep 17 00:00:00 2001 From: d4vidi Date: Mon, 21 Nov 2016 11:10:15 +0200 Subject: [PATCH] Add various fixes --- README.md | 20 +++++++++++++----- android/build.gradle | 2 +- .../core/notification/PushNotification.java | 6 +++++- .../notification/PushNotificationProps.java | 21 ++++++++++--------- .../INotificationsDrawerApplication.java | 4 +++- .../PushNotificationsDrawer.java | 2 +- example/android/myapplication/build.gradle | 3 ++- example/android/settings.gradle | 4 ++-- index.android.js | 4 ++++ 9 files changed, 44 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 962278d..078c950 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 97f3bf2..236cfe6 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 9d8fd28..c47efe1 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 d4512ce..b155dfa 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 fe17c74..c11c32d 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 7623f8f..9b3383b 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 00e378e..f97a9e8 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 ac703f3..9ac9552 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 e0517bc..36d4292 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 { -- 2.26.2