Commit e7f0ad93 authored by Libin Lu's avatar Libin Lu

merge from master

parents 1f1da63f 56f8bbc0
......@@ -9,6 +9,7 @@
- Change the _Bundle Identifier_ in Xcode project settings
- Select your _Team_ for both targets (`SimpleFcmClient` and `SimpleFcmClientTests`)
- Update your API_KEY [here](https://github.com/evollu/react-native-fcm/blob/master/Examples/simple-fcm-client/app/FirebaseConstants.js#L3)
- run `pod install` under `ios` folder
## Android
......
......@@ -133,6 +133,10 @@ android {
dependencies {
compile(project(':react-native-maps')) {
exclude group: 'com.google.android.gms', module: 'play-services-base'
// This resolution make compiler ignoring play-service-base's version requirement in react-native-maps
// so that it only read from react-native-fcm
// you can also lock the version in this gradle file and ignore all module declaration
// or you can use ResolutionStragety
}
compile project(':react-native-fcm')
compile fileTree(dir: "libs", include: ["*.jar"])
......
......@@ -10,6 +10,7 @@ import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import java.util.Arrays;
import java.util.List;
......@@ -36,4 +37,10 @@ public class MainApplication extends Application implements ReactApplication {
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
@Override
public void onCreate() { // <-- Check this block exists
super.onCreate();
SoLoader.init(this, /* native exopackage */ false); // <-- Check this line exists within the block
}
}
This diff is collapsed.
......@@ -6,7 +6,7 @@ const API_URL = "https://fcm.googleapis.com/fcm/send";
class FirebaseClient {
async send(body, type) {
if(FirebaseClient.KEY === 'YOUR_API_KEY'){
if(FirebaseConstants.KEY === 'YOUR_API_KEY'){
Alert.alert('Set your API_KEY in app/FirebaseConstants.js')
return;
}
......
......@@ -49,7 +49,7 @@ export function registerAppListener(navigation){
console.log("Notification", notif);
if(Platform.OS ==='ios' && notif._notificationType === NotificationType.WillPresent && !notif.local_notification){
// this notification is only to decide if you want to show the notification when user if in forground.
// this notification is only to decide if you want to show the notification when user if in foreground.
// usually you can ignore it. just decide to show or not.
notif.finish(WillPresentNotificationResult.All)
return;
......
......@@ -8,8 +8,8 @@
},
"dependencies": {
"react": "16.0.0-alpha.12",
"react-native": "^0.47.2",
"react-native-fcm": "^14.1.0",
"react-native": "^0.55.4",
"react-native-fcm": "^15.0.1",
"react-native-maps": "^0.20.1",
"react-navigation": "^1.2.1"
},
......
This source diff could not be displayed because it is too large. You can view the blob instead.
[![Join the chat at https://gitter.im/evollu/react-native-fcm](https://badges.gitter.im/evollu/react-native-fcm.svg)](https://gitter.im/evollu/react-native-fcm?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
## NOTES:
- current latest version: v10.x
[react-native-firebase](https://github.com/invertase/react-native-firebase/releases/tag/v4.0.0) has introduced new firebase messaging and remote/local notification features. It has good integration with other firebase features. I would recommend new comers to check that.
Note that there are some advanced features still in progress
- handle iOS remote notification when app is not running
- custom iOS notification actions
### To existing react-native-fcm users
`react-native-firebase` now can do what `react-native-fcm` can so it is a waste of effort to build the same thing in parallel.
Since I'm getting busier these days and start feeling challenging to maintain this repo every day while `react-native-firebase` has a larger team/company backing it, existing users may consider migrating to `react-native-firebase`.
I've created [an example project](https://github.com/evollu/react-native-fcm/tree/firebase/Examples/firebase-migration) using react-native-firebase as a migration reference
`react-native-fcm` will still take PRs and bug fixes, but possibly no new feature developement any more.
### Versions
- for iOS SDK < 4, use react-native-fcm@6.2.3 (v6.x is still compatible with Firebase SDK v4)
- for RN < 0.40.0, use react-native-fcm@2.5.6
- for RN < 0.33.0, use react-native-fcm@1.1.0
- for RN < 0.30.0, use react-native-fcm@1.0.15
- local notification is not only available in V1
### Example
- An example working project is available at: https://github.com/evollu/react-native-fcm/tree/master/Examples/simple-fcm-client
### Android 26
- DO NOT change Android targetSdkVersion >= 26. The notification won't show up because of notification channel requirement.
If you have to upgrade, you can use sdk-26 branch and post feedback on [here](https://github.com/evollu/react-native-fcm/pull/699)
......@@ -18,6 +36,7 @@ If you have to upgrade, you can use sdk-26 branch and post feedback on [here](ht
- Run `npm install react-native-fcm --save`
- [Link libraries](https://facebook.github.io/react-native/docs/linking-libraries-ios.html)
Note: the auto link doesn't work with xcworkspace so CocoaPods user needs to do manual linking
- You many need [this package for huawei phone](https://github.com/pgengoux/react-native-huawei-protected-apps)
## Configure Firebase Console
### FCM config file
......@@ -318,7 +337,7 @@ NOTE: `com.evollu.react.fcm.FIRLocalMessagingPublisher` is required for presenti
### Build custom push notification for Android
Firebase android misses important feature of android notification like `group`, `priority` and etc. As a work around you can send data message (no `notification` payload at all) and this repo will build a local notification for you. If you pass `custom_notification` in the payload, the repo will treat the content as a local notification config and shows immediately.
NOTE: By using this work around, you will have to send different types of payload for iOS and Android devices because custom_notification isn't supported on iOS
NOTE: By using this work around, you will have to send different types of payload for iOS and Android devices because **custom_notification isn't supported on iOS**
WARNING: `custom_notification` **cannot** be used together with `notification` attribute. use `data` **ALONE**
......@@ -342,6 +361,9 @@ Example of payload that is sent to FCM server:
}
```
Example of payload that is sent through firebase console:
<img width="753" alt="screen shot 2018-04-04 at 1 18 09 pm" src="https://user-images.githubusercontent.com/9213224/38323255-bbbfdd66-380a-11e8-95c2-bf32ced959b8.png">
Check local notification guide below for configuration.
**IMPORTANT**: When using the `admin.messaging` API, you need to `JSON.stringify` the `custom_notification` value:
......@@ -530,8 +552,11 @@ It is up to you! FCM is just a bridging library that passes notification into ja
#### I want to show notification when app is in foreground
Use `show_in_foreground` attribute to tell app to show banner even if the app is in foreground.
NOTE: this flag doesn't work for Android push notification, use `custom_notification` to achieve this.
NOTE: foreground notification is not available on iOS 9 and below
#### Do I need to handle APNS token registration?
No. Method swizzling in Firebase Cloud Messaging handles this unless you turn that off. Then you are on your own to implement the handling. Check this link https://firebase.google.com/docs/cloud-messaging/ios/client
......@@ -547,6 +572,12 @@ Issues and pull requests are welcome. Let's make this thing better!
#### Credits
Local notification implementation is inspired by react-native-push-notification by zo0r
#### I get the notifications in the logs, but the native prompt does not show up
Did you remember to ask the user permissions? ;)
```js
await FCM.requestPermissions({ badge: false, sound: true, alert: true })
```
## Sending remote notification
How to send a push notification from your server? You should `POST` to this endpoint:
......@@ -607,4 +638,3 @@ fetch('https://fcm.googleapis.com/fcm/send', {
})
})
```
apply plugin: 'com.android.library'
def DEFAULT_COMPILE_SDK_VERSION = 26
def DEFAULT_BUILD_TOOLS_VERSION = "25.0.2"
def DEFAULT_TARGET_SDK_VERSION = 26
def DEFAULT_GOOGLE_PLAY_SERVICES_VERSION = "12.+"
android {
compileSdkVersion 26
buildToolsVersion "25.0.2"
compileSdkVersion project.hasProperty('compileSdkVersion') ? project.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
buildToolsVersion project.hasProperty('buildToolsVersion') ? project.buildToolsVersion : DEFAULT_BUILD_TOOLS_VERSION
defaultConfig {
minSdkVersion 16
targetSdkVersion 26
targetSdkVersion project.hasProperty('buildToolsVetargetSdkVersionrsion') ? project.buildToolsVersion : DEFAULT_TARGET_SDK_VERSION
versionCode 1
versionName "1.0"
}
......@@ -17,10 +22,12 @@ repositories {
}
dependencies {
def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : DEFAULT_GOOGLE_PLAY_SERVICES_VERSION
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.facebook.react:react-native:+'
compile 'com.google.firebase:firebase-core:+'
compile 'com.google.firebase:firebase-messaging:+'
compile "com.google.firebase:firebase-core:$googlePlayServicesVersion"
compile "com.google.firebase:firebase-messaging:$googlePlayServicesVersion"
compile 'me.leolin:ShortcutBadger:1.1.17@aar'
compile "com.android.support:support-core-utils:26.1.0"
}
......
......@@ -30,6 +30,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import static com.facebook.react.common.ReactConstants.TAG;
......@@ -55,9 +56,11 @@ public class SendNotificationTask extends AsyncTask<Void, Void, Void> {
return null;
}
if (bundle.getString("body") == null) {
String body = bundle.getString("body");
if (body == null) {
return null;
}
body = URLDecoder.decode( body, "UTF-8" );
Resources res = mContext.getResources();
String packageName = mContext.getPackageName();
......@@ -67,20 +70,29 @@ public class SendNotificationTask extends AsyncTask<Void, Void, Void> {
ApplicationInfo appInfo = mContext.getApplicationInfo();
title = mContext.getPackageManager().getApplicationLabel(appInfo).toString();
}
title = URLDecoder.decode( title, "UTF-8" );
String ticker = bundle.getString("ticker");
if (ticker != null) ticker = URLDecoder.decode( ticker, "UTF-8" );
String subText = bundle.getString("sub_text");
if (subText != null) subText = URLDecoder.decode( subText, "UTF-8" );
NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext, bundle.getString("channel"))
.setContentTitle(title)
.setContentText(bundle.getString("body"))
.setTicker(bundle.getString("ticker"))
.setContentText(body)
.setTicker(ticker)
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.setAutoCancel(bundle.getBoolean("auto_cancel", true))
.setNumber((int)bundle.getDouble("number"))
.setSubText(bundle.getString("sub_text"))
.setNumber(bundle.getInt("number", (int)bundle.getDouble("number")))
.setSubText(subText)
.setVibrate(new long[]{0, DEFAULT_VIBRATION})
.setExtras(bundle.getBundle("data"));
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
notification.setGroup(bundle.getString("group"));
String group = bundle.getString("group");
if (group != null) group = URLDecoder.decode( group, "UTF-8" );
notification.setGroup(group);
}
if (bundle.containsKey("ongoing") && bundle.getBoolean("ongoing")) {
......@@ -132,11 +144,13 @@ public class SendNotificationTask extends AsyncTask<Void, Void, Void> {
//big text
String bigText = bundle.getString("big_text");
if(bigText != null){
bigText = URLDecoder.decode( bigText, "UTF-8" );
notification.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText));
}
//picture
String picture = bundle.getString("picture");
if(picture!=null){
NotificationCompat.BigPictureStyle bigPicture = new NotificationCompat.BigPictureStyle();
......@@ -151,9 +165,8 @@ public class SendNotificationTask extends AsyncTask<Void, Void, Void> {
bigPicture.bigPicture(pictureResIdBitmap);
}
}
bigPicture.setBigContentTitle(title);
bigPicture.setSummaryText(bundle.getString("body"));
// setBigContentTitle and setSummaryText overrides current title with body and subtext
// that cause to display duplicated body in subtext when picture has specified
notification.setStyle(bigPicture);
}
......@@ -209,7 +222,11 @@ public class SendNotificationTask extends AsyncTask<Void, Void, Void> {
intent.setClassName(mContext, intentClassName);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtras(bundle);
intent.setAction(bundle.getString("click_action"));
String clickAction = bundle.getString("click_action");
if (clickAction != null) clickAction = URLDecoder.decode( clickAction, "UTF-8" );
intent.setAction(clickAction);
int notificationID = bundle.containsKey("id") ? bundle.getString("id", "").hashCode() : (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, notificationID, intent,
......@@ -218,7 +235,10 @@ public class SendNotificationTask extends AsyncTask<Void, Void, Void> {
notification.setContentIntent(pendingIntent);
if (bundle.containsKey("android_actions")) {
WritableArray actions = ReactNativeJson.convertJsonToArray(new JSONArray(bundle.getString("android_actions")));
String androidActions = bundle.getString("android_actions");
androidActions = URLDecoder.decode( androidActions, "UTF-8" );
WritableArray actions = ReactNativeJson.convertJsonToArray(new JSONArray(androidActions));
for (int a = 0; a < actions.size(); a++) {
ReadableMap action = actions.getMap(a);
String actionTitle = action.getString("title");
......@@ -282,4 +302,3 @@ public class SendNotificationTask extends AsyncTask<Void, Void, Void> {
return launchIntent != null ? launchIntent.getComponent().getClassName() : null;
}
}
......@@ -229,6 +229,7 @@ RCT_MULTI_ENUM_CONVERTER(UNNotificationCategoryOptions, (@{
static bool jsHandlerRegistered;
static NSMutableArray* pendingNotifications;
static NSString* refreshToken;
RCT_EXPORT_MODULE();
......@@ -326,6 +327,9 @@ RCT_EXPORT_MODULE();
if(!jsHandlerRegistered){
[self sendPendingNotifications];
}
if(refreshToken != nil){
[self sendEventWithName:FCMTokenRefreshed body:refreshToken];
}
});
return self;
......@@ -336,6 +340,9 @@ RCT_EXPORT_MODULE();
if([eventName isEqualToString:FCMNotificationReceived]) {
[self sendPendingNotifications];
} else if([eventName isEqualToString:FCMTokenRefreshed] && refreshToken != nil) {
[self sendEventWithName:FCMTokenRefreshed body:refreshToken];
refreshToken = nil;
}
}
......@@ -448,7 +455,8 @@ RCT_EXPORT_METHOD(deleteInstanceId:(RCTPromiseResolveBlock)resolve rejecter:(RCT
}];
}
- (void)messaging:(nonnull FIRMessaging *)messaging didRefreshRegistrationToken:(nonnull NSString *)fcmToken {
- (void)messaging:(nonnull FIRMessaging *)messaging didReceiveRegistrationToken:(nonnull NSString *)fcmToken {
refreshToken = fcmToken;
[self sendEventWithName:FCMTokenRefreshed body:fcmToken];
}
......@@ -504,8 +512,7 @@ RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic)
[[FIRMessaging messaging] unsubscribeFromTopic:topic];
}
// Receive data message on iOS 10 devices.
- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
- (void)messaging:(FIRMessaging *)messaging didReceiveMessage:(FIRMessagingRemoteMessage *)remoteMessage {
[self sendEventWithName:FCMNotificationReceived body:[remoteMessage appData]];
}
......
......@@ -24,5 +24,5 @@
"type": "git",
"url": "git+https://github.com/evollu/react-native-fcm.git"
},
"version": "14.1.2"
"version": "15.0.1"
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment