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

Commit

Permalink
Issue #17: Custom notification sound in background mode?
Browse files Browse the repository at this point in the history
  • Loading branch information
macdonst committed Jul 24, 2015
1 parent dc15f50 commit 17173e9
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 135 deletions.
44 changes: 38 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,27 @@ Produces the following notification.

![2015-07-24 02 17 55](https://cloud.githubusercontent.com/assets/353180/8866900/2df0ab06-3190-11e5-9a81-fdb85bb0f5a4.png)

### Sound

In order for your your notification to play a custom sound you will need to add the files to your Android project's `res/raw` directory. Then send the follow JSON from GCM:

```javascript
{
title:"Sound Test",
message: "Loaded res/raw",
soundname: "test"
}
```

*Note:* when you specify the custom sound file name omit the file's extension.

### Stacking

By default when using this plugin on Android each notification that your app receives will replace the previous notification in the shade.

If you want to see multiple notifications in the shade you will need to provide a notification ID as part of the push data sent to the app. For instance if you send:

```
```javascript
{
title: "Test Push",
message: "Push number 1"
Expand All @@ -255,7 +268,7 @@ If you want to see multiple notifications in the shade you will need to provide

Followed by:

```
```javascript
{
title: "Test Push",
message: "Push number 2"
Expand All @@ -264,7 +277,7 @@ Followed by:

You will only see "Push number 2" in the shade. However, if you send:

```
```javascript
{
title: "Test Push",
message: "Push number 1",
Expand All @@ -274,7 +287,7 @@ You will only see "Push number 2" in the shade. However, if you send:

and:

```
```javascript
{
title: "Test Push",
message: "Push number 2",
Expand All @@ -284,15 +297,34 @@ and:

You will only see both "Push number 1" and "Push number 2" in the shade.

## Windows Notifications
## iOS Behaviour

### Sound

In order for your your notification to play a custom sound you will need to add the files to root of your iOS project. The files must be in the proper format. See the [Local and Remote Notification Programming Guide](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW6) for more info on proper file formats and how to convert existing sound files.

Then send the follow JSON from APNS:

```javascript
{
"aps": {
"alert": "Test sound",
"sound": "sub.caf"
}
}
```

## Windows Behaviour

###Notifications

The plugin supports all types of windows platform notifications namely [Tile, Toast, Badge and Raw](https://msdn.microsoft.com/en-us/library/windows/apps/Hh779725.aspx). The API supports the basic cases of the notification templates with title corresponding to the first text element and message corresponding to the second if title is present else the first one. The image corresponds to the first image element of the notification xml.

The count is present only for the badge notification in which it represent the value of the notification which could be a number from 0-99 or a status glyph.

For advanced templates and usage, the notification object is included in [`data.additionalData.pushNotificationReceivedEventArgs`](https://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.pushnotifications.pushnotificationreceivedeventargs).

## Setting Toast Capable Option for Windows
### Setting Toast Capable Option for Windows

For your app to communicate through a toast notification, you must declare that it is Toast Capable in your app's manifest file. Cordova-windows 4.0.0 release adds this property to config.xml. You can use:
`<preference name="WindowsToastCapable" value="true" />` in config.xml. However, you will need Cordova 5.1.1 which pins Cordova-windows 4.0.0.
Expand Down
14 changes: 11 additions & 3 deletions src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
Expand All @@ -20,6 +21,7 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
Expand Down Expand Up @@ -103,13 +105,12 @@ public void createNotification(Context context, Bundle extras) {

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setDefaults(defaults)
//.setDefaults(defaults)
.setWhen(System.currentTimeMillis())
.setContentTitle(extras.getString("title"))
.setTicker(extras.getString("title"))
.setContentIntent(contentIntent)
.setAutoCancel(true);


SharedPreferences prefs = context.getSharedPreferences("com.adobe.phonegap.push", Context.MODE_PRIVATE);
String localIcon = prefs.getString("icon", null);
Expand Down Expand Up @@ -198,7 +199,14 @@ public void createNotification(Context context, Bundle extras) {
}
}


String soundname = extras.getString("soundname");
if (soundname != null) {
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + context.getPackageName() + "/raw/" + soundname);
Log.d(LOG_TAG, sound.toString());
mBuilder.setSound(sound);
}

String message = extras.getString("message");
if (message != null) {
mBuilder.setContentText(message);
Expand Down
Loading

0 comments on commit 17173e9

Please sign in to comment.