Created
October 6, 2020 10:13
-
-
Save gryzzly/76558aeeb7fde43287ab1d67475f742e to your computer and use it in GitHub Desktop.
React Native Android JSON parameters for Intents
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
public class NotificationsModule extends ReactContextBaseJavaModule { | |
private static String channelID = "8888"; | |
private static String channelTitle = "Notifications"; | |
private ReactApplicationContext reactContext; | |
public static final String NAME = "Notifications"; | |
NotificationsModule(ReactApplicationContext context) { | |
super(context); | |
reactContext = context; | |
registerBroadcastReceiver(); | |
} | |
@ReactMethod | |
public void show(ReadableMap details) { | |
Intent notificationIntent = new Intent( | |
"Notification" | |
); | |
// identifier is required | |
notificationIntent.putExtra("identifier", details.getInt("identifier")); | |
if (details.hasKey("body")) { | |
notificationIntent.putExtra("body", details.getString("body")); | |
} | |
// Here’s a nested object and we can use Arguments.toBundle in order to attach it to an intent | |
if (details.hasKey("userInfo")) { | |
notificationIntent.putExtra("userInfo", Arguments.toBundle(details.getMap("userInfo"))); | |
} | |
PendingIntent pendingIntent = PendingIntent.getBroadcast( | |
reactContext, | |
0, | |
notificationIntent, | |
PendingIntent.FLAG_ONE_SHOT | |
); | |
AlarmManager alarmManager = | |
(AlarmManager) reactContext.getSystemService(Context.ALARM_SERVICE); | |
if (!details.hasKey("interval")) { | |
Log.e("Notifications", "Time interval must be provided and be greater than 0"); | |
return; | |
} | |
int triggerAt = details.getInt("interval"); | |
alarmManager.setExactAndAllowWhileIdle( | |
AlarmManager.RTC_WAKEUP, | |
System.currentTimeMillis() + triggerAt, | |
pendingIntent | |
); | |
} | |
private final BroadcastReceiver notificationsReceiver = new BroadcastReceiver() { | |
@RequiresApi(api = Build.VERSION_CODES.O) | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Bundle extra = intent.getExtras(); | |
if (com.mad.dance.MainActivity.isRunning) { | |
WritableMap params = Arguments.createMap(); | |
for (String key : extra.keySet()) { | |
if (key.equals("identifier")) { | |
params.putInt(key, extra.getInt(key)); | |
} else if (key.equals("userInfo")) { | |
// turn bundle data into ReadableMap (become JS object on JS side) again | |
params.putMap("userInfo", Arguments.makeNativeMap(extra.getBundle(key))); | |
} else { | |
params.putString(key, extra.getString(key)); | |
} | |
} | |
reactContext | |
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) | |
.emit("Notification", params); | |
} else { | |
NotificationCompat.Builder b = new NotificationCompat.Builder(context, channelID); | |
Intent openAppIntent = new Intent(reactContext, com.mad.dance.MainActivity.class); | |
PendingIntent openAppPendingIntent = PendingIntent.getActivity( | |
reactContext, | |
0, | |
openAppIntent, | |
0 | |
); | |
b | |
.setContentIntent(openAppPendingIntent) | |
.setSmallIcon(R.mipmap.ic_launcher) | |
.setAutoCancel(true); | |
int identifier = extra.getInt("identifier"); | |
String body = extra.getString("body"); | |
if (body != null && !body.trim().isEmpty()) { | |
b.setContentText(body); | |
} | |
NotificationManager notificationManager = | |
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | |
// It's safe to call this repeatedly because creating an existing notification | |
// channel performs no operation. | |
// https://developer.android.com/training/notify-user/build-notification | |
NotificationChannel notificationChannel = new NotificationChannel( | |
channelID, | |
channelTitle, | |
NotificationManager.IMPORTANCE_HIGH | |
); | |
notificationManager.createNotificationChannel(notificationChannel); | |
notificationManager.notify(identifier, b.build()); | |
} | |
} | |
}; | |
private void registerBroadcastReceiver() { | |
IntentFilter filter = new IntentFilter("Notification"); | |
reactContext.registerReceiver(notificationsReceiver, filter); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment