Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 84d07d3
Showing
11 changed files
with
441 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# react-native-gcm-android | ||
|
||
GCM for React Native Android |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
apply plugin: 'com.android.library' | ||
apply plugin: 'com.google.gms.google-services' | ||
|
||
android { | ||
compileSdkVersion 23 | ||
buildToolsVersion "23.0.1" | ||
|
||
defaultConfig { | ||
minSdkVersion 16 | ||
targetSdkVersion 22 | ||
versionCode 1 | ||
versionName "1.0" | ||
ndk { | ||
abiFilters "armeabi-v7a", "x86" | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
compile fileTree(include: ['*.jar'], dir: 'libs') | ||
compile 'com.facebook.react:react-native:0.14.+' | ||
compile 'com.google.android.gms:play-services:8.1.0' | ||
compile 'com.android.support:appcompat-v7:23.0.0' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.oney.gcm"> | ||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.oney.gcm; | ||
|
||
import android.app.Activity; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Intent; | ||
import android.content.IntentFilter; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
|
||
import com.facebook.react.bridge.Arguments; | ||
import com.facebook.react.bridge.LifecycleEventListener; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.Callback; | ||
import com.facebook.react.bridge.ReactContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.bridge.UiThreadUtil; | ||
import com.facebook.react.bridge.WritableMap; | ||
import com.facebook.react.bridge.ReadableMap; | ||
import com.facebook.react.bridge.ReadableArray; | ||
import com.facebook.react.modules.core.DeviceEventManagerModule; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import org.json.*; | ||
import android.content.SharedPreferences; | ||
import android.preference.PreferenceManager; | ||
|
||
import android.util.Log; | ||
|
||
import android.content.Context; | ||
|
||
public class GcmModule extends ReactContextBaseJavaModule { | ||
private final static String TAG = GcmModule.class.getCanonicalName(); | ||
private ReactContext mReactContext; | ||
|
||
public GcmModule(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
mReactContext = reactContext; | ||
listenGcmRegistration(); | ||
listenGcmReceiveNotification(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "GcmModule"; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getConstants() { | ||
final Map<String, Object> constants = new HashMap<>(); | ||
return constants; | ||
} | ||
|
||
private void sendEvent(String eventName, Object params) { | ||
mReactContext | ||
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) | ||
.emit(eventName, params); | ||
} | ||
|
||
private void listenGcmRegistration() { | ||
IntentFilter intentFilter = new IntentFilter("RNGCMRegisteredToken"); | ||
|
||
mReactContext.registerReceiver(new BroadcastReceiver() { | ||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
String token = intent.getStringExtra("token"); | ||
WritableMap params = Arguments.createMap(); | ||
params.putString("deviceToken", token); | ||
|
||
sendEvent("remoteNotificationsRegistered", params); | ||
} | ||
}, intentFilter); | ||
} | ||
|
||
private String convertJSON(Bundle bundle) { | ||
JSONObject json = new JSONObject(); | ||
Set<String> keys = bundle.keySet(); | ||
for (String key : keys) { | ||
try { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | ||
json.put(key, JSONObject.wrap(bundle.get(key))); | ||
} else { | ||
json.put(key, bundle.get(key)); | ||
} | ||
} catch(JSONException e) { | ||
return null; | ||
} | ||
} | ||
return json.toString(); | ||
} | ||
|
||
private void listenGcmReceiveNotification() { | ||
IntentFilter intentFilter = new IntentFilter("RNGCMReceiveNotification"); | ||
|
||
mReactContext.registerReceiver(new BroadcastReceiver() { | ||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
Bundle bundle = intent.getBundleExtra("bundle"); | ||
|
||
String bundleString = convertJSON(bundle); | ||
|
||
WritableMap params = Arguments.createMap(); | ||
params.putString("dataJSON", bundleString); | ||
|
||
sendEvent("remoteNotificationReceived", params); | ||
} | ||
}, intentFilter); | ||
} | ||
|
||
@ReactMethod | ||
public void requestPermissions() { | ||
mReactContext.startService(new Intent(mReactContext, GcmRegistrationService.class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.oney.gcm; | ||
|
||
import android.app.Activity; | ||
|
||
import com.facebook.react.ReactPackage; | ||
import com.facebook.react.bridge.JavaScriptModule; | ||
import com.facebook.react.bridge.NativeModule; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.uimanager.ViewManager; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class GcmPackage implements ReactPackage { | ||
|
||
public GcmPackage(){ | ||
} | ||
|
||
@Override | ||
public List<NativeModule> createNativeModules( | ||
ReactApplicationContext reactContext) { | ||
List<NativeModule> modules = new ArrayList<>(); | ||
|
||
modules.add(new GcmModule(reactContext)); | ||
return modules; | ||
} | ||
|
||
@Override | ||
public List<Class<? extends JavaScriptModule>> createJSModules() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) { | ||
return Arrays.<ViewManager>asList(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
android/src/main/java/com/oney/gcm/GcmRegistrationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.oney.gcm; | ||
|
||
import android.app.IntentService; | ||
import android.content.Intent; | ||
import android.content.SharedPreferences; | ||
import android.content.res.Resources; | ||
import android.preference.PreferenceManager; | ||
import android.support.v4.content.LocalBroadcastManager; | ||
import android.util.Log; | ||
|
||
import com.google.android.gms.gcm.GoogleCloudMessaging; | ||
import com.google.android.gms.iid.InstanceID; | ||
|
||
import java.io.IOException; | ||
|
||
public class GcmRegistrationService extends IntentService { | ||
|
||
private static final String TAG = "GcmRegistrationService"; | ||
|
||
public GcmRegistrationService() { | ||
super(TAG); | ||
} | ||
|
||
@Override | ||
protected void onHandleIntent(Intent intent) { | ||
Resources resources = getApplication().getResources(); | ||
String packageName = getApplication().getPackageName(); | ||
|
||
int resourceId = resources.getIdentifier("gcm_defaultSenderId", "string", packageName); | ||
String projectNumber = getString(resourceId); | ||
|
||
if (projectNumber == null) { | ||
return; | ||
} | ||
|
||
try { | ||
InstanceID instanceID = InstanceID.getInstance(this); | ||
String token = instanceID.getToken(projectNumber, | ||
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); | ||
Log.i(TAG, "GCM Registration Token: " + token); | ||
|
||
registeredToken(token); | ||
|
||
} catch (Exception e) { | ||
Log.d(TAG, "Failed to complete token refresh", e); | ||
} | ||
} | ||
private void registeredToken(String token) { | ||
Intent i = new Intent("RNGCMRegisteredToken"); | ||
i.putExtra("token", token); | ||
sendBroadcast(i); | ||
} | ||
|
||
} |
108 changes: 108 additions & 0 deletions
108
android/src/main/java/com/oney/gcm/RNGcmListenerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.oney.gcm; | ||
|
||
import android.app.Notification; | ||
import android.app.NotificationManager; | ||
import android.app.PendingIntent; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.media.RingtoneManager; | ||
import android.net.Uri; | ||
import android.os.Bundle; | ||
import android.support.v4.app.NotificationCompat; | ||
import android.util.Log; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.content.res.Resources; | ||
import android.app.ActivityManager; | ||
import android.app.ActivityManager.RunningAppProcessInfo; | ||
import java.util.List; | ||
|
||
import com.google.android.gms.gcm.GcmListenerService; | ||
|
||
public class RNGcmListenerService extends GcmListenerService { | ||
|
||
private static final String TAG = "RNGcmListenerService"; | ||
|
||
@Override | ||
public void onMessageReceived(String from, Bundle bundle) { | ||
sendNotification(bundle); | ||
} | ||
|
||
public Class getMainActivityClass() { | ||
try { | ||
String packageName = getApplication().getPackageName(); | ||
return Class.forName(packageName + ".MainActivity"); | ||
} catch (ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
private boolean applicationIsRunning() { | ||
ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); | ||
List<RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses(); | ||
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) { | ||
if (processInfo.processName.equals(getApplication().getPackageName())) { | ||
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { | ||
for (String d: processInfo.pkgList) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private void sendNotification(Bundle bundle) { | ||
Resources resources = getApplication().getResources(); | ||
|
||
String packageName = getApplication().getPackageName(); | ||
|
||
Class intentClass = getMainActivityClass(); | ||
if (intentClass == null) { | ||
return; | ||
} | ||
|
||
if (applicationIsRunning()) { | ||
Intent i = new Intent("RNGCMReceiveNotification"); | ||
i.putExtra("bundle", bundle); | ||
sendBroadcast(i); | ||
return; | ||
} | ||
|
||
int resourceId = resources.getIdentifier(bundle.getString("largeIcon"), "mipmap", packageName); | ||
|
||
Intent intent = new Intent(this, intentClass); | ||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | ||
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, | ||
PendingIntent.FLAG_ONE_SHOT); | ||
|
||
Bitmap largeIcon = BitmapFactory.decodeResource(resources, resourceId); | ||
|
||
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); | ||
|
||
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) | ||
.setLargeIcon(largeIcon) | ||
.setSmallIcon(R.drawable.ic_cast_dark) | ||
.setContentTitle(bundle.getString("contentTitle")) | ||
.setContentText(bundle.getString("message")) | ||
.setAutoCancel(false) | ||
.setSound(defaultSoundUri) | ||
.setTicker(bundle.getString("ticker")) | ||
.setCategory(NotificationCompat.CATEGORY_CALL) | ||
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE) | ||
.setPriority(NotificationCompat.PRIORITY_HIGH) | ||
.setContentIntent(pendingIntent); | ||
|
||
NotificationManager notificationManager = | ||
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | ||
|
||
Notification notif = notificationBuilder.build(); | ||
notif.defaults |= Notification.DEFAULT_VIBRATE; | ||
notif.defaults |= Notification.DEFAULT_SOUND; | ||
notif.defaults |= Notification.DEFAULT_LIGHTS; | ||
|
||
notificationManager.notify(0, notif); | ||
} | ||
} |
Oops, something went wrong.