Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

use explicit setPackage and use LocalBroadcastManager #3

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ allprojects {
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion 26
buildToolsVersion "26.0.3"

defaultConfig {
minSdkVersion 16
targetSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
ndk {
Expand All @@ -39,7 +39,7 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:appcompat-v7:26.1.+'
compile 'com.facebook.react:react-native:+'
compile 'com.google.android.gms:play-services-gcm:+'
compile 'me.leolin:ShortcutBadger:1.1.8@aar'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import android.app.Activity;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;

import com.dieam.reactnativepushnotification.helpers.ApplicationBadgeHelper;
import com.facebook.react.bridge.ActivityEventListener;
Expand All @@ -26,7 +31,17 @@
import java.util.Random;

public class RNPushNotification extends ReactContextBaseJavaModule implements ActivityEventListener {
public static final String LOG_TAG = "RNPushNotification";// all logging should use this tag
public static final String LOG_TAG = "RNPushNotification"; // all logging should use this tag

// Hardcode these channel parameters for now; ideally, we'd be
// able to plumb this through from PushNotification.configure.

public static final String OLD_CHANNEL_ID = "keybase_channel_all";
public static final String CHANNEL_ID = "keybase_all";
public static final String CHANNEL_SOUNDNAME = "keybasemessage";

private static final String CHANNEL_NAME = "Keybase";
private static final int CHANNEL_IMPORTANCE = NotificationManager.IMPORTANCE_HIGH;

private RNPushNotificationHelper mRNPushNotificationHelper;
private final Random mRandomNumberGenerator = new Random(System.currentTimeMillis());
Expand All @@ -44,6 +59,27 @@ public RNPushNotification(ReactApplicationContext reactContext) {
mJsDelivery = new RNPushNotificationJsDelivery(reactContext);

registerNotificationsRegistration();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager manager =
(NotificationManager) reactContext.getSystemService(reactContext.NOTIFICATION_SERVICE);
// Delete old notification channel
manager.deleteNotificationChannel(OLD_CHANNEL_ID);

// Create notification channel
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, CHANNEL_IMPORTANCE);

// Add soundname
int resId = reactContext.getResources().getIdentifier(CHANNEL_SOUNDNAME, "raw", reactContext.getPackageName());
Uri soundUri = Uri.parse("android.resource://" + reactContext.getPackageName() + "/" + resId);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.build();
channel.setSound(soundUri, audioAttributes);

manager.createNotificationChannel(channel);
}
}

@Override
Expand All @@ -70,7 +106,7 @@ public void onNewIntent(Intent intent) {
private void registerNotificationsRegistration() {
IntentFilter intentFilter = new IntentFilter(getReactApplicationContext().getPackageName() + ".RNPushNotificationRegisteredToken");

getReactApplicationContext().registerReceiver(new BroadcastReceiver() {
LocalBroadcastManager.getInstance(getReactApplicationContext()).registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String token = intent.getStringExtra("token");
Expand All @@ -89,7 +125,7 @@ private void registerNotificationsReceiveNotificationActions(ReadableArray actio
String action = actions.getString(i);
intentFilter.addAction(getReactApplicationContext().getPackageName() + "." + action);
}
getReactApplicationContext().registerReceiver(new BroadcastReceiver() {
LocalBroadcastManager.getInstance(getReactApplicationContext()).registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getBundleExtra("notification");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private PendingIntent toScheduleNotificationIntent(Bundle bundle) {
int notificationID = Integer.parseInt(bundle.getString("id"));

Intent notificationIntent = new Intent(context, RNPushNotificationPublisher.class);
notificationIntent.setPackage(context.getPackageName());
notificationIntent.putExtra(RNPushNotificationPublisher.NOTIFICATION_ID, notificationID);
notificationIntent.putExtras(bundle);

Expand Down Expand Up @@ -157,7 +158,7 @@ public void sendToNotificationCentre(Bundle bundle) {
title = context.getPackageManager().getApplicationLabel(appInfo).toString();
}

NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, RNPushNotification.CHANNEL_ID)
.setContentTitle(title)
.setTicker(bundle.getString("ticker"))
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
Expand Down Expand Up @@ -225,6 +226,7 @@ public void sendToNotificationCentre(Bundle bundle) {
notification.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText));

Intent intent = new Intent(context, intentClass);
intent.setPackage(context.getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
bundle.putBoolean("userInteraction", true);
intent.putExtra("notification", bundle);
Expand Down Expand Up @@ -304,6 +306,7 @@ public void sendToNotificationCentre(Bundle bundle) {
}

Intent actionIntent = new Intent();
actionIntent.setPackage(context.getPackageName());
actionIntent.setAction(context.getPackageName() + "." + action);
// Add "action" for later identifying which button gets pressed.
bundle.putString("action", action);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.google.android.gms.gcm.GoogleCloudMessaging;
Expand Down Expand Up @@ -33,6 +34,6 @@ protected void onHandleIntent(Intent intent) {
private void sendRegistrationToken(String token) {
Intent intent = new Intent(this.getPackageName() + ".RNPushNotificationRegisteredToken");
intent.putExtra("token", token);
sendBroadcast(intent);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}