Skip to content

Commit

Permalink
Fix for required notification channel on Oreo
Browse files Browse the repository at this point in the history
  • Loading branch information
keyz182 committed Mar 19, 2018
1 parent 196a660 commit c1c3805
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ xmlns:android="http://schemas.android.com/apk/res/android">
<receiver android:name="org.apache.cordova.firebase.OnNotificationOpenReceiver"></receiver>
</config-file>
<resource-file src="src/android/google-services.json" target="."/>
<resource-file src="src/android/cordova-plugin-firebase-strings.xml" target="res/values/cordova-plugin-firebase-strings.xml" />
<source-file src="src/android/FirebasePlugin.java" target-dir="src/org/apache/cordova/firebase" />
<source-file src="src/android/OnNotificationOpenReceiver.java" target-dir="src/org/apache/cordova/firebase" />
<source-file src="src/android/FirebasePluginInstanceIDService.java" target-dir="src/org/apache/cordova/firebase" />
Expand Down
14 changes: 13 additions & 1 deletion src/android/FirebasePluginMessagingService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.apache.cordova.firebase;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
Expand All @@ -13,6 +14,7 @@
import android.text.TextUtils;
import android.content.ContentResolver;

import com.asb360.area.dev.R;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

Expand Down Expand Up @@ -93,8 +95,10 @@ private void sendNotification(String id, String title, String messageBody, Map<S
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id.hashCode(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);

String channelId = getString(R.string.default_notification_channel_id);
String channelName = getString(R.string.default_notification_channel_name);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setContentTitle(title)
.setContentText(messageBody)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
Expand Down Expand Up @@ -136,6 +140,14 @@ private void sendNotification(String id, String title, String messageBody, Map<S
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// Since android Oreo notification channel is needed.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
channelName,
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}

notificationManager.notify(id.hashCode(), notification);
} else {
bundle.putBoolean("tap", false);
Expand Down
5 changes: 5 additions & 0 deletions src/android/cordova-plugin-firebase-strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<resources>
<string name="default_notification_channel_id">fcm_default_channel</string>
<string name="default_notification_channel_name">Default</string>
</resources>

0 comments on commit c1c3805

Please sign in to comment.