Skip to content

Instantly share code, notes, and snippets.

@gryzzly
Last active September 2, 2020 11:30
Show Gist options
  • Select an option

  • Save gryzzly/d026be33c56f30f8cf2455b84757f258 to your computer and use it in GitHub Desktop.

Select an option

Save gryzzly/d026be33c56f30f8cf2455b84757f258 to your computer and use it in GitHub Desktop.

Revisions

  1. gryzzly revised this gist Sep 2, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Notifications.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    package com.mad.dance;
    package com.foo.bar;

    import android.util.Log;

  2. gryzzly created this gist Sep 2, 2020.
    85 changes: 85 additions & 0 deletions Notifications.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    package com.mad.dance;

    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");
    }


    }