This PhoneGap plugin for Android is based on the phonegap plugin found here, and the tips found at this blog post, updated to work with PhoneGap 3. This plugin must be manually installed (for now).
I had to update the old plugin for a project of mine, so I figured I would share the code and steps I took so others could use the plugin too :)
-
Create a basic phonegap project using the PhoneGap CLI.
-
Add LocalNotification.js to your /www/js directory.
-
In /platforms/android/src/, create a com folder if it doesn't exist. Inside of that, create a phonegap folder. Inside of that, create a plugin folder. Finally, inside of that, create the localnotification folder.
-
Copy all the .java files into this last folder.
-
Open AlarmReceiver.java and change
import com.phonegap.your.project.namespace.R;
so that it uses your app's package name instead. Remember to keep the .R at the end of the line! -
Open /platforms/android/res/xml/config.xml and add the following to the end of the
<feature>
list (inside the<widget>
tag):
<feature name="LocalNotification">
<param name="android-package" value="com.phonegap.plugin.localnotification.LocalNotification" />
</feature>
- Open /platforms/android/AndroidManifest.xml and add the following inside the
<application>
tag:
<receiver android:name="com.phonegap.plugin.localnotification.AlarmReceiver" >
</receiver>
<receiver android:name="com.phonegap.plugin.localnotification.AlarmRestoreOnBoot" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Also add the following to your permissions list (inside the manifest
tag):
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
This is so your app can restore any notifications it set after a system reboot.
- Lastly, reference LocalNotification.js in your www/index.html or wherever your main application code is:
<script type="text/javascript" src="js/LocalNotification.js"></script>
Here's an example of a notification being set to go off a minute from now:
var d = new Date();
d = d.getTime() + 3*1000;
d = new Date(d)
plugins.localNotification.add({
date : d,
message : "Phonegap - Local Notification\r\nSubtitle comes after linebreak",
ticker : "This is a sample ticker text",
repeatDaily : false,
id : 4
});
Some other functions you can use:
plugins.localNotification.add({ date: new Date(), message: 'This is an Android alarm using the statusbar', id: 123 });
plugins.localNotification.cancel(123);
plugins.localNotification.cancelAll();
- This plugin must be added manually and doesn't work with PhoneGap Build. This means you have to use the CLI to build and deploy your app (use phonegap local run android)