Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Neson/react-native-system-notific…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
zetavg committed Feb 19, 2016
2 parents 8cdb944 + bbf2a7f commit a7c7815
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 75 deletions.
143 changes: 78 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ Send or schedule Android system notifications for React Native.

<img width="35%" align="right" hspace="1" vspace="1" src="http://i.imgur.com/cY2Z9GH.png"></img>

## Table of Contents

* [Installation](#installation)
* [Usage](#usage)
* [Creating Notifications](#creating-notifications)
* [Basic](#basic)
* [Scheduling](#scheduling)
* [Customization](#customization)
* [Handle Notification Click Event](#handle-notification-click-event)
* [Manage Scheduled Notifications](#manage-scheduled-notifications)
* [Clearing Notifications](#clearing-notifications)
* [Push Notifications On Android](#push-notifications-on-android)

---

```js
import React, { DeviceEventEmitter } from 'react-native';
Expand Down Expand Up @@ -164,43 +178,41 @@ Notification.create({

All available options on a notification are listed below:

**Basic**
#### Basic

**id (`number`)**
The unique ID of this notification. It will be randomly chosen if not specified.

<dl>
<dt>id (`number`)</dt>
<dd>The unique ID of this notification. It will be randomly chosen if not specified.</dd>
**subject (`string`)**
The notification subject. Defaults to the application name on Android.

<dt>subject (`string`)</dt>
<dd>The notification subject. Defaults to the application name on Android.</dd>
**message (`string`)**
The message showen in the notification.

<dt>message (`string`)</dt>
<dd>The message showen in the notification.</dd>
**action (`string`)**
An action name that can be used to determine what to do when this notification is clicked. Defaults to `DEFAULT`.

<dt>action (`string`)</dt>
<dd>An action name that can be used to determine what to do when this notification is clicked. Defaults to `DEFAULT`.</dd>
**payload (`object`)**
A custom payload object. It can be retrieved on events of this notification. Defaults to `{}`.

<dt>payload (`object`)</dt>
<dd>A custom payload object. It can be retrieved on events of this notification. Defaults to `{}`.</dd>
</dl>

**Scheduling**
#### Scheduling

<dl>
<dt>delay (`number`)</dt>
<dd>Milliseconds to delay before showing this notification after it is created. Useful when creating countdown alarms, reminders, etc. Note that it cannot be used with `sendAt`.</dd>
**delay (`number`)**
Milliseconds to delay before showing this notification after it is created. Useful when creating countdown alarms, reminders, etc. Note that it cannot be used with `sendAt`.

<dt>sendAt (`Date`)</dt>
<dd>Schedule this notification to show on a specified time. Note that it cannot be used with `delay`.</dd>
**sendAt (`Date`)**
Schedule this notification to show on a specified time. Note that it cannot be used with `delay`.

<dt>repeatEvery (`string` or `number`)</dt>
<dd>Must use with `sendAt`. Schedule this notification to repeat. Can be `minute`, `hour`, `halfDay`, `day`, `week`, `month`, `year` or a number of time in milliseconds.</dd>
**repeatEvery (`string` or `number`)**
Must use with `sendAt`. Schedule this notification to repeat. Can be `minute`, `hour`, `halfDay`, `day`, `week`, `month`, `year` or a number of time in milliseconds.

<dt>repeatCount (`number`)</dt>
<dd>Must use with `sendAt` and `repeatEvery`. End repeating this notification after n times. Note that it cannot be used with `endAt`.</dd>
**repeatCount (`number`)**
Must use with `sendAt` and `repeatEvery`. End repeating this notification after n times. Note that it cannot be used with `endAt`.

**endAt (`Date`)**
Must use with `sendAt` and `repeatEvery`. End repeating this notification after a specified time. Note that it cannot be used with `repeatCount`.

<dt>endAt (`Date`)</dt>
<dd>Must use with `sendAt` and `repeatEvery`. End repeating this notification after a specified time. Note that it cannot be used with `repeatCount`.</dd>
</dl>

> Some Samples of Scheduled Notifications
>
Expand Down Expand Up @@ -232,63 +244,64 @@ All available options on a notification are listed below:
> });
> ```

**Customization**
#### Customization

**priority (`number`)**
Priority of this notification, can be `-2`, `-1`, `0`, `1`, `2`. When this is set to `1` or `2`, heads-up notification will be more likely to show on Android 5+. Defaults to `1`.

<dl>
<dt>priority (`number`)</dt>
<dd>Priority of this notification, can be `-2`, `-1`, `0`, `1`, `2`. When this is set to `1` or `2`, heads-up notification will be more likely to show on Android 5+. Defaults to `1`.</dd>
**smallIcon (`string`)**
The icon (file name) to show. This icon must be placed in the project's `android/app/src/main/res/mipmap-*` folder. Defaults to `ic_launcher`.

<dt>smallIcon (`string`)</dt>
<dd>The icon (file name) to show. This icon must be placed in the project's `android/app/src/main/res/mipmap-*` folder. Defaults to `ic_launcher`.</dd>
**largeIcon (`string`)**
Not yet implemented.

<dt>largeIcon (`string`)</dt>
<dd>Not yet implemented.</dd>
**sound (`string`)**
Set the sound to play. Defaults to `default` as using the default notification sound, or set this to `null` to disable the sound. Other options are not yet implemented.

<dt>sound (`string`)</dt>
<dd>Set the sound to play. Defaults to `default` as using the default notification sound, or set this to `null` to disable the sound. Other options are not yet implemented.</dd>
**vibrate (`string`)**
Set the vibration pattern to use. Defaults to `default` as using the default notification vibrate, or set this to `null` to disable the vibrate. Other options are not yet implemented.

<dt>vibrate (`string`)</dt>
<dd>Set the vibration pattern to use. Defaults to `default` as using the default notification vibrate, or set this to `null` to disable the vibrate. Other options are not yet implemented.</dd>
**lights (`string`)**
Set the desired color for the indicator LED on the device. Defaults to `default` as using the default notification lights, or set this to `null` to disable the lights. Other options are not yet implemented.

<dt>lights (`string`)</dt>
<dd>Set the desired color for the indicator LED on the device. Defaults to `default` as using the default notification lights, or set this to `null` to disable the lights. Other options are not yet implemented.</dd>
**autoClear (`boolean`)**
Clear this notification automatically after the user clicks on it. Defaults to `true`.

<dt>autoClear (`boolean`)</dt>
<dd>Clear this notification automatically after the user clicks on it. Defaults to `true`.</dd>
**onlyAlertOnce (`boolean`)**
Do not let the sound, vibrate and ticker to be played if the notification is already showing.

<dt>onlyAlertOnce (`boolean`)</dt>
<dd>Do not let the sound, vibrate and ticker to be played if the notification is already showing.</dd>
**tickerText (`string`)**
Set the text to show on ticker. Defaults to `<subject>: <message>`. Set this to `null` to disable ticker.

<dt>tickerText (`string`)</dt>
<dd>Set the text to show on ticker. Defaults to `<subject>: <message>`. Set this to `null` to disable ticker.</dd>
**when (`Date`)**
Add a timestamp pertaining to the notification (usually the time the event occurred).

<dt>when (`Date`)</dt>
<dd>Add a timestamp pertaining to the notification (usually the time the event occurred).</dd>
**bigText (`string`)**
Set the text to be shown when the user expand the notification.

<dt>subText (`string`)</dt>
<dd>Set the third line of text in the platform notification template. Note that it cannot be used with `progress`.</dd>
**subText (`string`)**
Set the third line of text in the platform notification template. Note that it cannot be used with `progress`.

<dt>progress (`number`)</dt>
<dd>Set the progress this notification represents, range: `0.0` ~ `1.0`. Set this to a number lower then zero to get an indeterminate progress. Note that it cannot be used with `subText`.</dd>
**progress (`number`)**
Set the progress this notification represents, range: `0.0` ~ `1.0`. Set this to a number lower then zero to get an indeterminate progress. Note that it cannot be used with `subText`.

<dt>color (`string`)</dt>
<dd>Not yet implemented. Color to be applied by the standard Style templates when presenting this notification.</dd>
**color (`string`)**
Color to be applied by the standard Style templates when presenting this notification.

<dt>number (`number`)</dt>
<dd>Set a number on the notification.</dd>
**number (`number`)**
Set a number on the notification.

<dt>private (`boolean`)</dt>
<dd>Not yet implemented.</dd>
**private (`boolean`)**
Not yet implemented.

<dt>ongoing (`boolean`)</dt>
<dd>Not yet implemented.</dd>
**ongoing (`boolean`)**
Not yet implemented.

<dt>category (`string`)</dt>
<dd>Set the notification category, e.g.: `alarm`, `call`, `email`, `event`, `progress`, `reminder`, `social`. It may be used by the Android system for ranking and filtering.</dd>
**category (`string`)**
Set the notification category, e.g.: `alarm`, `call`, `email`, `event`, `progress`, `reminder`, `social`. It may be used by the Android system for ranking and filtering.

<dt>localOnly (`boolean`)</dt>
<dd>Set whether or not this notification should not bridge to other devices.</dd>
</dl>
**localOnly (`boolean`)**
Set whether or not this notification should not bridge to other devices.

### Handle Notification Click Event

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.neson.react.notification.NotificationPublisher;

import android.util.Log;
import android.graphics.Color;

/**
* An object-oriented Wrapper class around the system notification class.
Expand Down Expand Up @@ -152,6 +153,16 @@ public android.app.Notification build() {
notificationBuilder.setShowWhen(true);
}

if (attributes.bigText != null) {
notificationBuilder
.setStyle(new android.app.Notification.BigTextStyle()
.bigText(attributes.bigText));
}

if (attributes.color != null) {
notificationBuilder.setColor(Color.parseColor(attributes.color));
}

if (attributes.subText != null) {
notificationBuilder.setSubText(attributes.subText);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class NotificationAttributes {
public Boolean onlyAlertOnce;
public String tickerText;
public Long when;
public String bigText;
public String subText;
public Integer progress;
public String color;
Expand Down Expand Up @@ -117,6 +118,7 @@ public void loadFromReadableMap(ReadableMap readableMap) {
if (readableMap.hasKey("onlyAlertOnce")) onlyAlertOnce = readableMap.getBoolean("onlyAlertOnce");
if (readableMap.hasKey("tickerText")) tickerText = readableMap.getString("tickerText");
if (readableMap.hasKey("when")) when = Long.parseLong(readableMap.getString("when"));
if (readableMap.hasKey("bigText")) bigText = readableMap.getString("bigText");
if (readableMap.hasKey("subText")) subText = readableMap.getString("subText");
if (readableMap.hasKey("progress")) progress = readableMap.getInt("progress");
if (readableMap.hasKey("color")) color = readableMap.getString("color");
Expand Down Expand Up @@ -162,6 +164,7 @@ public ReadableMap asReadableMap() {
if (onlyAlertOnce != null) writableMap.putBoolean("onlyAlertOnce", onlyAlertOnce);
if (tickerText != null) writableMap.putString("tickerText", tickerText);
if (when != null) writableMap.putString("when", Long.toString(when));
if (bigText != null) writableMap.putString("bigText", bigText);
if (subText != null) writableMap.putString("subText", subText);
if (progress != null) writableMap.putInt("progress", progress);
if (color != null) writableMap.putString("color", color);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void onReceive(Context context, Intent intent) {

Log.i("ReactSystemNotification", "NotificationEventReceiver: Recived: " + extras.getString(ACTION) + ", Notification ID: " + extras.getInt(NOTIFICATION_ID) + ", payload: " + extras.getString(PAYLOAD));

// If the application is not running, start it with the notification
// If the application is not running or is not in foreground, start it with the notification
// passed in
if (!applicationIsRunning(context)) {
String packageName = context.getApplicationContext().getPackageName();
Expand All @@ -41,20 +41,25 @@ public void onReceive(Context context, Intent intent) {
launchIntent.putExtra("initialSysNotificationPayload", extras.getString(PAYLOAD));

context.startActivity(launchIntent);

Log.i("ReactSystemNotification", "NotificationEventReceiver: Launching: " + packageName);

// If the application is running, send a brodcast
sendBroadcast(context, extras); // Send a broadcast after come from background to foreground
} else {
Intent brodcastIntent = new Intent("NotificationEvent");
brodcastIntent.putExtra("id", extras.getInt(NOTIFICATION_ID));
brodcastIntent.putExtra("action", extras.getString(ACTION));
brodcastIntent.putExtra("payload", extras.getString(PAYLOAD));
context.sendBroadcast(brodcastIntent);
Log.v("ReactSystemNotification", "NotificationEventReceiver: Broadcast Sent: NotificationEvent: " + extras.getString(ACTION) + ", Notification ID: " + extras.getInt(NOTIFICATION_ID) + ", payload: " + extras.getString(PAYLOAD));
sendBroadcast(context, extras); // If the application is already running in foreground, send a brodcast too
}
}

private void sendBroadcast(Context context, Bundle extras) {
Intent brodcastIntent = new Intent("NotificationEvent");

brodcastIntent.putExtra("id", extras.getInt(NOTIFICATION_ID));
brodcastIntent.putExtra("action", extras.getString(ACTION));
brodcastIntent.putExtra("payload", extras.getString(PAYLOAD));

context.sendBroadcast(brodcastIntent);
Log.v("ReactSystemNotification", "NotificationEventReceiver: Broadcast Sent: NotificationEvent: " + extras.getString(ACTION) + ", Notification ID: " + extras.getInt(NOTIFICATION_ID) + ", payload: " + extras.getString(PAYLOAD));
}

private boolean applicationIsRunning(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
],
"peerDependencies": {
"react-native": ">=0.15.0",
"react-native-gcm-android": "^0.1.8"
"react-native-gcm-android": "^0.2.0"
}
}

0 comments on commit a7c7815

Please sign in to comment.