Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
Add ongoing notifications for android (#1996)
Browse files Browse the repository at this point in the history
* ✨ add function for setting ongoing parameter

* 📝 Update payload documentation for ongoing parameter

* 🎨 improve code style of function 'setNotificationOngoing'
  • Loading branch information
Sebastian authored and macdonst committed Oct 18, 2017
1 parent 28f7089 commit c1e29c1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
44 changes: 44 additions & 0 deletions docs/PAYLOAD.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [Huawei and Xiaomi Phones](#huawei-and-xiaomi-phones)
- [Application force closed](#application-force-closed)
- [Visibility](#visibility-of-notifications)
- [Ongoing Notifications](#ongoing-notifications)
- [Badges](#badges)
- [Support for Twilio Notify](#support-for-twilio-notify)
- [Notification ID](#notification-id)
Expand Down Expand Up @@ -1390,6 +1391,49 @@ fcm.send(message, (err, response) => {
});
```

## Ongoing Notifications

Set whether this is an "ongoing" notification. Ongoing notifications cannot be dismissed by the user, so your application or service must take care of canceling them. They are typically used to indicate a background task that the user is actively engaged with (e.g., playing music) or is pending in some way and therefore occupying the device (e.g., a file download, sync operation, active network connection).

```javascript
{
"registration_ids": ["my device id"],
"data": {
"title": "This is an ongoing Notification",
"message": "Some people also call me a sticky notification",
"ongoing": true
}
}
```

Here is an example using fcm-node that sends the above JSON:

```javascript
var FCM = require('fcm-node');
// Replace these with your own values.
var apiKey = "replace with API key";
var deviceID = "my device id";
var fcm = new FCM(apiKey);

var message = {
to: deviceID,
data: {
title: 'This is an ongoing Notification',
message: 'Some people also call me a sticky notification',
ongoing: true
}
};

fcm.send(message, function(err, response){
if (err) {
console.log(err);
console.log("Something has gone wrong!");
} else {
console.log("Successfully sent with response: ", response);
}
});
```

## Badges

On Android not all launchers support badges. In order for us to set badges we use [ShortcutBadger](https://github.com/leolin310148/ShortcutBadger) in order to set the badge. Check out their website to see which launchers are supported.
Expand Down
10 changes: 10 additions & 0 deletions src/android/com/adobe/phonegap/push/FCMService.java
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,11 @@ public void createNotification(Context context, Bundle extras) {
*/
setNotificationCount(context, extras, mBuilder);

/*
* Notification ongoing
*/
setNotificationOngoing(extras, mBuilder);

/*
* Notification count
*/
Expand Down Expand Up @@ -633,6 +638,11 @@ private void setNotificationVibration(Bundle extras, Boolean vibrateOption, Noti
}
}

private void setNotificationOngoing(Bundle extras, NotificationCompat.Builder mBuilder) {
boolean ongoing = extras.optBoolean(ONGOING, false);
mBuilder.setOngoing(ongoing);
}

private void setNotificationMessage(int notId, Bundle extras, NotificationCompat.Builder mBuilder) {
String message = extras.getString(MESSAGE);
String style = extras.getString(STYLE, STYLE_TEXT);
Expand Down
1 change: 1 addition & 0 deletions src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@ public interface PushConstants {
public static final String CREATE_CHANNEL = "create";
public static final String DELETE_CHANNEL = "remove";
public static final String UPDATE_CHANNEL = "update";
public static final String ONGOING = "ongoing";
}

0 comments on commit c1e29c1

Please sign in to comment.