Skip to content

Commit

Permalink
feat: use Firebase SDK for creating foreground notifications (#1657)
Browse files Browse the repository at this point in the history
  • Loading branch information
tafelnl committed Sep 7, 2023
1 parent eb503fe commit 530029c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 47 deletions.
21 changes: 21 additions & 0 deletions push-notifications/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ If no icon is specified Android will use the application icon, but push icon sho

Android Studio has an icon generator you can use to create your Push Notifications icon.

## Push Notification channel

From Android 8.0 (API level 26) and higher, notification channels are supported and recommended. The SDK will derive the `channelId` for incoming push notifications in the following order:

1. **Firstly it will check if the incoming notification has a `channelId` set.**
When sending a push notification from either the FCM dashboard, or through their API, it's possible to specify a `channelId`.
2. **Then it will check for a possible given value in the `AndroidManifest.xml`.**
If you prefer to create and use your own default channel, set `default_notification_channel_id` to the ID of your notification channel object as shown; FCM will use this value whenever incoming messages do not explicitly set a notification channel.

```xml
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
```

3. **Lastly it will use the fallback `channelId` that the Firebase SDK provides for us.**
FCM provides a default notification channel with basic settings out of the box. This channel will be created by the Firebase SDK upon receiving the first push message.

> **Warning**
> When using option 1 or 2, you are still required to create a notification channel in code with an ID that matches the one used the chosen option. You can use [`createChannel(...)`](#createchannel) for this. If you don't do this, the SDK will fallback to option 3.
## Push notifications appearance in foreground

<docgen-config>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

public class NotificationChannelManager {

public static final String FOREGROUND_NOTIFICATION_CHANNEL_ID = "PushDefaultForeground";

private Context context;
private NotificationManager notificationManager;
private PluginConfig config;
Expand All @@ -26,7 +24,6 @@ public NotificationChannelManager(Context context, NotificationManager manager,
this.context = context;
this.notificationManager = manager;
this.config = config;
createForegroundNotificationChannel();
}

private static String CHANNEL_ID = "id";
Expand Down Expand Up @@ -140,35 +137,4 @@ public void listChannels(PluginCall call) {
call.unavailable();
}
}

/**
* Create notification channel
*/
public void createForegroundNotificationChannel() {
// Create the NotificationChannel only if presentationOptions is defined
// Because the channel can't be changed after creation
String[] presentation = config.getArray("presentationOptions");
if (presentation != null) {
// And only on API 26+ because the NotificationChannel class
// is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Push Notifications Foreground";
String description = "Push notifications in foreground";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(FOREGROUND_NOTIFICATION_CHANNEL_ID, name, importance);
channel.setDescription(description);
if (Arrays.asList(presentation).contains("sound")) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
channel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, audioAttributes);
}
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
android.app.NotificationManager notificationManager = context.getSystemService(android.app.NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
import android.os.Build;
import android.os.Bundle;
import android.service.notification.StatusBarNotification;
import androidx.core.app.NotificationCompat;
import com.getcapacitor.*;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.getcapacitor.annotation.Permission;
import com.getcapacitor.annotation.PermissionCallback;
import com.google.firebase.messaging.CommonNotificationBuilder;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.NotificationParams;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Arrays;
import org.json.JSONException;
Expand Down Expand Up @@ -270,20 +271,25 @@ public void fireNotification(RemoteMessage remoteMessage) {
bundle = getBundleLegacy();
}

int pushIcon = android.R.drawable.ic_dialog_info;
if (bundle != null) {
NotificationParams params = new NotificationParams(remoteMessage.toIntent().getExtras());

if (bundle != null && bundle.getInt("com.google.firebase.messaging.default_notification_icon") != 0) {
pushIcon = bundle.getInt("com.google.firebase.messaging.default_notification_icon");
String channelId = CommonNotificationBuilder.getOrCreateChannel(
getContext(),
params.getNotificationChannelId(),
bundle
);

CommonNotificationBuilder.DisplayNotificationInfo notificationInfo = CommonNotificationBuilder.createNotificationInfo(
getContext(),
getContext(),
params,
channelId,
bundle
);

notificationManager.notify(notificationInfo.tag, notificationInfo.id, notificationInfo.notificationBuilder.build());
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getContext(),
NotificationChannelManager.FOREGROUND_NOTIFICATION_CHANNEL_ID
)
.setSmallIcon(pushIcon)
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationManager.notify(0, builder.build());
}
}
remoteMessageData.put("title", title);
Expand Down

0 comments on commit 530029c

Please sign in to comment.