Skip to content

Instantly share code, notes, and snippets.

@gryzzly
Last active September 2, 2020 11:30
Show Gist options
  • Save gryzzly/d026be33c56f30f8cf2455b84757f258 to your computer and use it in GitHub Desktop.
Save gryzzly/d026be33c56f30f8cf2455b84757f258 to your computer and use it in GitHub Desktop.
React Native Notifications.java, show a notification
package com.foo.bar;
import android.util.Log;
import android.content.Context;
import android.provider.Settings;
import java.lang.System;
import android.app.Notification;
import androidx.core.app.NotificationCompat;
import android.app.NotificationManager;
import android.app.NotificationChannel;
import android.content.Intent;
import android.app.PendingIntent;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
public class Notifications extends ReactContextBaseJavaModule {
private static ReactApplicationContext reactContext;
Notifications(ReactApplicationContext context) {
super(context);
reactContext = context;
}
@Override
public String getName() {
return "Notifications";
}
@ReactMethod
public void show(ReadableMap details) {
Log.v("my-debug", "notifications show start");
Intent notificationIntent = new Intent(
reactContext,
MainActivity.class
);
PendingIntent pendingIntent = PendingIntent.getActivity(
reactContext,
0,
notificationIntent,
Intent.FLAG_ACTIVITY_NEW_TASK
);
NotificationCompat.Builder b = new NotificationCompat.Builder(reactContext);
b
.setContentIntent(pendingIntent)
.setContentText("Body text not working")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setChannelId("10000");
if (details.hasKey("body")) {
b.setContentText(details.getString("body"));
}
NotificationManager notificationManager =
(NotificationManager) reactContext.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(
"10000",
"NOTIFICATION_CHANNEL_NAME",
NotificationManager.IMPORTANCE_HIGH
);
notificationManager.createNotificationChannel(notificationChannel) ;
notificationManager.notify(1, b.build());
Log.v("my-debug", "notifications show reached end");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment