From a5ff4e028c5efbea4b30938ad7d6c2b7bf529918 Mon Sep 17 00:00:00 2001 From: askilondz Date: Tue, 9 Jan 2018 10:44:48 -0600 Subject: [PATCH 1/2] fix: Added a NotificationChannel to the NotificationManager as required now for Android Oreo in order to display push notifications. Note this is a hot fix...in the future will want to add ability for the developer to specify notification channels. --- src/android/GcmReceiver.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/android/GcmReceiver.java b/src/android/GcmReceiver.java index dd700c1..06b656b 100644 --- a/src/android/GcmReceiver.java +++ b/src/android/GcmReceiver.java @@ -2,6 +2,7 @@ import android.app.Notification; import android.app.NotificationManager; +import android.app.NotificationChannel; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; @@ -9,6 +10,7 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; +import android.os.Build; import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.support.v4.content.ContextCompat; @@ -66,6 +68,11 @@ public void createNotification(Context context, Bundle extras) String channel = extras.getString("C"); String message = extras.getString("message"); + String CHANNEL_ID = "cp_channel_01"; //The id of the channel. + CharSequence name = "Notifications"; // The user-visible name of the channel. + int importance = NotificationManager.IMPORTANCE_HIGH; + NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance); + int largeIcon = getIcon(context, "large_notification_icon"); Bitmap appIcon = BitmapFactory.decodeResource(context.getResources(), largeIcon); @@ -74,7 +81,7 @@ public void createNotification(Context context, Bundle extras) int smallIcon = getIcon(context, "small_notification_icon"); NotificationCompat.Builder mBuilder = - new NotificationCompat.Builder(context) + new NotificationCompat.Builder(context, CHANNEL_ID) .setDefaults(defaults) .setPriority(getAppPriority(context)) .setLargeIcon(appIcon) @@ -107,6 +114,10 @@ public void createNotification(Context context, Bundle extras) Log.e(TAG, "Number format exception - Error parsing Notification ID" + e.getMessage()); } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + mNotificationManager.createNotificationChannel(mChannel); + } + mNotificationManager.notify(appName, notId, mBuilder.build()); } From 724c1e8ccef9092899e00043126dfdad1018991d Mon Sep 17 00:00:00 2001 From: askilondz Date: Wed, 24 Jan 2018 17:50:28 -0600 Subject: [PATCH 2/2] fix: checking android version before declaring NotificationChannel --- src/android/GcmReceiver.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/GcmReceiver.java b/src/android/GcmReceiver.java index 06b656b..79043ec 100644 --- a/src/android/GcmReceiver.java +++ b/src/android/GcmReceiver.java @@ -71,7 +71,6 @@ public void createNotification(Context context, Bundle extras) String CHANNEL_ID = "cp_channel_01"; //The id of the channel. CharSequence name = "Notifications"; // The user-visible name of the channel. int importance = NotificationManager.IMPORTANCE_HIGH; - NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance); int largeIcon = getIcon(context, "large_notification_icon"); @@ -115,6 +114,7 @@ public void createNotification(Context context, Bundle extras) } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance); mNotificationManager.createNotificationChannel(mChannel); }