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

Commit

Permalink
Issue #158: Notification Event Not Firing When Closed Through App Lau…
Browse files Browse the repository at this point in the history
…ncher

* Use Cordova-Android 6 for start in background feature
* Finish start in background
* Update docs
  • Loading branch information
macdonst committed Nov 2, 2016
1 parent 4340290 commit ca18653
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 43 deletions.
29 changes: 2 additions & 27 deletions docs/INSTALLATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,6 @@ For more detailed instructions on how to install the Android Support Library vis
android update sdk --no-ui --filter "extra"
```

### Notification after forced closing

To use payload parameter "force-start" this code must be added in MainActivity.java:

```
boolean startOnBackground = false;
Bundle extras = getIntent().getExtras();
if (extras != null) {
startOnBackground = extras.getBoolean(PushConstants.START_ON_BACKGROUND);
if(startOnBackground) {
moveTaskToBack(true);
}
}
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
super.loadUrl("javascript: { window.startOnBackground = "+(startOnBackground?1:0)+"; }");
```

right after

```
super.onCreate(savedInstanceState);
```

### Co-existing with Facebook Plugin

There are a number of Cordova Facebook Plugins available but the one that we recommend is [Jeduan's fork](https://github.com/jeduan/cordova-plugin-facebook4) of the original Wizcorp plugin. It is setup to use Gradle/Maven and the latest Facebook SDK properly.
Expand Down Expand Up @@ -241,9 +216,9 @@ Required `cordova-cli` version: `6.4.0`

Required `cordova-ios` version: `4.3.0`

Version `1.9.0` (and above) of this plugin supports [CocoaPods](https://cocoapods.org) installation of the [Google Cloud Messaging](https://cocoapods.org/pods/GoogleCloudMessaging) library.
Version `1.9.0` (and above) of this plugin supports [CocoaPods](https://cocoapods.org) installation of the [Google Cloud Messaging](https://cocoapods.org/pods/GoogleCloudMessaging) library.

If you are installing this plugin using `npm`, and you are using version `6.1.0` or greater of the `cordova-cli`, it will automatically download the right version of this plugin for both your platform and cli.
If you are installing this plugin using `npm`, and you are using version `6.1.0` or greater of the `cordova-cli`, it will automatically download the right version of this plugin for both your platform and cli.

If you are on a `cordova-cli` version less than `6.1.0`, you will either have to upgrade your `cordova-cli` version, or install the plugin explicitly:

Expand Down
31 changes: 30 additions & 1 deletion docs/PAYLOAD.md
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,36 @@ These phones have a particular quirk that when the app is force closed that you

### Application force closed

It is possible to add `force-start: true` to the data payload to restart application in background even if force closed.
If you add `force-start: 1` to the data payload the application will be restarted in background even if it was force closed.

```javascript
{
"registration_ids": ["my device id"],
"data": {
"title": "Force Start",
"message": "This notification should restart the app",
"force-start": 1
}
}
```

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

```javascript
var gcm = require('node-gcm');
// Replace these with your own values.
var apiKey = "replace with API key";
var deviceID = "my device id";
var service = new gcm.Sender(apiKey);
var message = new gcm.Message();
message.addData('title', 'Force Start');
message.addData('message', 'This notification should restart the app');
message.addData('force-start', 1);
service.send(message, { registrationTokens: [ deviceID ] }, function (err, response) {
if(err) console.error(err);
else console.log(response);
});
```

## Visibility of Notifications

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@
"cordova-ios": ">=4.1.0"
},
"1.9.0": {
"cordova-ios": ">=4.3.0",
"cordova-ios": ">=4.3.0",
"cordova-android": ">=6.0.0",
"cordova": ">=6.4.0"
},
"2.0.0": {
"cordova": ">100"
"cordova": ">100"
}
},
"author": "Adobe PhoneGap Team",
Expand Down
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</js-module>
<engines>
<engine name="cordova" version=">=6.4.0"/>
<engine name="cordova-android" version=">=4.0.0"/>
<engine name="cordova-android" version=">=6.0.0"/>
<engine name="cordova-ios" version=">=4.3.0"/>
</engines>
<preference name="SENDER_ID"/>
Expand Down Expand Up @@ -102,7 +102,7 @@
<framework src="libz.tbd"/>
<framework src="GoogleCloudMessaging" type="podspec" spec="~> 1.2.0"/>
<framework src="GGLInstanceID" type="podspec" spec="~> 1.2.1"/>

</platform>
<platform name="windows">
<hook type="after_plugin_install" src="hooks/windows/setToastCapable.js"/>
Expand Down
18 changes: 10 additions & 8 deletions src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private void showNotificationIfPossible (Context context, Bundle extras) {
String message = extras.getString(MESSAGE);
String title = extras.getString(TITLE);
String contentAvailable = extras.getString(CONTENT_AVAILABLE);
String forceStart = "1"; //extras.getString(FORCE_START);
String forceStart = extras.getString(FORCE_START);
int badgeCount = extractBadgeCount(extras);
if (badgeCount >= 0) {
Log.d(LOG_TAG, "count =[" + badgeCount + "]");
Expand All @@ -287,6 +287,7 @@ private void showNotificationIfPossible (Context context, Bundle extras) {
Log.d(LOG_TAG, "message =[" + message + "]");
Log.d(LOG_TAG, "title =[" + title + "]");
Log.d(LOG_TAG, "contentAvailable =[" + contentAvailable + "]");
Log.d(LOG_TAG, "forceStart =[" + forceStart + "]");

if ((message != null && message.length() != 0) ||
(title != null && title.length() != 0)) {
Expand All @@ -300,18 +301,19 @@ private void showNotificationIfPossible (Context context, Bundle extras) {
createNotification(context, extras);
}

if ("1".equals(contentAvailable)) {
Log.d(LOG_TAG, "send notification event");
PushPlugin.sendExtras(extras);
}

if(!PushPlugin.isActive() && "1".equals(forceStart)){
Log.d(LOG_TAG, "app is not running but we should start it and put in background");
Intent intent = new Intent(this, PushHandlerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(PUSH_BUNDLE, extras);
intent.putExtra(START_ON_BACKGROUND, true);
intent.putExtra(START_IN_BACKGROUND, true);
intent.putExtra(FOREGROUND, false);
startActivity(intent);
}
} else if ("1".equals(contentAvailable)) {
Log.d(LOG_TAG, "app is not running and content available true");
Log.d(LOG_TAG, "send notification event");
PushPlugin.sendExtras(extras);
}
}

public void createNotification(Context context, Bundle extras) {
Expand Down
2 changes: 1 addition & 1 deletion src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ public interface PushConstants {
public static final String TWILIO_BODY = "twi_body";
public static final String TWILIO_TITLE = "twi_title";
public static final String TWILIO_SOUND = "twi_sound";
public static final String START_ON_BACKGROUND = "start-on-background";
public static final String START_IN_BACKGROUND = "cdvStartInBackground";
public static final String FORCE_START = "force-start";
}
4 changes: 2 additions & 2 deletions src/android/com/adobe/phonegap/push/PushHandlerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void onCreate(Bundle savedInstanceState) {
String callback = getIntent().getExtras().getString("callback");
Log.d(LOG_TAG, "callback = " + callback);
boolean foreground = getIntent().getExtras().getBoolean("foreground", true);
boolean startOnBackground = getIntent().getExtras().getBoolean(START_ON_BACKGROUND, true);
boolean startOnBackground = getIntent().getExtras().getBoolean(START_IN_BACKGROUND, false);

if(!startOnBackground){
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Expand Down Expand Up @@ -101,7 +101,7 @@ private void forceMainActivityReload(boolean startOnBackground) {
}
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
launchIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
launchIntent.putExtra(START_ON_BACKGROUND, true);
launchIntent.putExtra(START_IN_BACKGROUND, startOnBackground);
}

startActivity(launchIntent);
Expand Down

0 comments on commit ca18653

Please sign in to comment.