Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cordova-plugin-local-notifications
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nabil Sadki
cordova-plugin-local-notifications
Commits
89015b59
Commit
89015b59
authored
Oct 05, 2013
by
Sebastián Katzer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Re-register the alarms with the AlarmManager since these alarms are lost in case of reboot
parent
9c40e8ef
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
87 additions
and
8 deletions
+87
-8
plugin.xml
plugin.xml
+17
-0
LocalNotification.java
src/android/LocalNotification.java
+13
-8
LocalNotificationRestore.java
src/android/LocalNotificationRestore.java
+57
-0
No files found.
plugin.xml
View file @
89015b59
...
...
@@ -44,7 +44,24 @@
</config-file>
<config-file
target=
"AndroidManifest.xml"
parent=
"/manifest/application"
>
<!--
* The alarm receiver is triggered when a scheduled alarm is fired. This class
* reads the information in the intent and displays this information in the
* Android notification bar. The notification uses the default notification
* sound and it vibrates the phone.
-->
<receiver
android:name=
"de.appplant.cordova.plugin.LocalNotificationReceiver"
/>
<!--
* This class is triggered upon reboot of the device. It needs to re-register
* the alarms with the AlarmManager since these alarms are lost in case of
* reboot.
-->
<receiver
android:name=
"de.appplant.cordova.plugin.LocalNotificationRestore"
>
<intent-filter>
<action
android:name=
"android.intent.action.BOOT_COMPLETED"
/>
</intent-filter>
</receiver>
</config-file>
<config-file
target=
"AndroidManifest.xml"
parent=
"/manifest"
>
...
...
src/android/LocalNotification.java
View file @
89015b59
...
...
@@ -23,6 +23,7 @@ import android.app.PendingIntent;
import
android.content.SharedPreferences
;
import
android.content.SharedPreferences.Editor
;
import
org.apache.cordova.CordovaInterface
;
import
org.apache.cordova.CordovaPlugin
;
import
org.apache.cordova.CallbackContext
;
...
...
@@ -36,11 +37,15 @@ public class LocalNotification extends CordovaPlugin {
public
static
final
String
PLUGIN_NAME
=
"LocalNotification"
;
public
static
CordovaInterface
cordova
=
null
;
@Override
public
boolean
execute
(
String
action
,
JSONArray
args
,
CallbackContext
callbackContext
)
throws
JSONException
{
JSONObject
arguments
=
args
.
getJSONObject
(
0
);
LocalNotificationOptions
options
=
new
LocalNotificationOptions
(
arguments
);
LocalNotification
.
cordova
=
super
.
cordova
;
String
alarmId
=
options
.
getId
();
if
(
action
.
equalsIgnoreCase
(
"add"
))
{
...
...
@@ -74,14 +79,13 @@ public class LocalNotification extends CordovaPlugin {
* @param options
* The options that can be specified per alarm.
*/
p
rivate
void
add
(
LocalNotificationOptions
options
)
{
p
ublic
static
void
add
(
LocalNotificationOptions
options
)
{
long
triggerTime
=
options
.
getDate
();
Intent
intent
=
new
Intent
(
cordova
.
getActivity
(),
LocalNotificationReceiver
.
class
);
intent
.
setAction
(
""
+
options
.
getId
());
intent
.
putExtra
(
LocalNotificationReceiver
.
OPTIONS
,
options
.
getJSONObject
().
toString
());
/* Get the AlarmManager service */
AlarmManager
am
=
getAlarmManager
();
PendingIntent
sender
=
PendingIntent
.
getBroadcast
(
cordova
.
getActivity
(),
0
,
intent
,
PendingIntent
.
FLAG_CANCEL_CURRENT
);
...
...
@@ -99,7 +103,7 @@ public class LocalNotification extends CordovaPlugin {
* The original ID of the notification that was used when it was
* registered using add()
*/
p
rivate
void
cancel
(
String
notificationId
)
{
p
ublic
static
void
cancel
(
String
notificationId
)
{
/*
* Create an intent that looks similar, to the one that was registered
* using add. Making sure the notification id in the action is the same.
...
...
@@ -126,7 +130,7 @@ public class LocalNotification extends CordovaPlugin {
* all our alarms to loop through these alarms and unregister them one
* by one.
*/
p
rivate
void
cancelAll
()
{
p
ublic
static
void
cancelAll
()
{
SharedPreferences
settings
=
cordova
.
getActivity
().
getSharedPreferences
(
PLUGIN_NAME
,
Context
.
MODE_PRIVATE
);
Map
<
String
,
?>
alarms
=
settings
.
getAll
();
Set
<
String
>
alarmIds
=
alarms
.
keySet
();
...
...
@@ -144,7 +148,7 @@ public class LocalNotification extends CordovaPlugin {
* @param args
* The assumption is that parse has been called already.
*/
p
rivate
void
persist
(
String
alarmId
,
JSONArray
args
)
{
p
ublic
static
void
persist
(
String
alarmId
,
JSONArray
args
)
{
Editor
editor
=
cordova
.
getActivity
().
getSharedPreferences
(
PLUGIN_NAME
,
Context
.
MODE_PRIVATE
).
edit
();
if
(
alarmId
!=
null
)
{
...
...
@@ -159,7 +163,7 @@ public class LocalNotification extends CordovaPlugin {
* @param alarmId
* The Id of the notification that must be removed.
*/
p
rivate
void
unpersist
(
String
alarmId
)
{
p
ublic
static
void
unpersist
(
String
alarmId
)
{
Editor
editor
=
cordova
.
getActivity
().
getSharedPreferences
(
PLUGIN_NAME
,
Context
.
MODE_PRIVATE
).
edit
();
if
(
alarmId
!=
null
)
{
...
...
@@ -171,14 +175,14 @@ public class LocalNotification extends CordovaPlugin {
/**
* Clear all alarms from the Android shared Preferences.
*/
p
rivate
void
unpersistAll
()
{
p
ublic
static
void
unpersistAll
()
{
Editor
editor
=
cordova
.
getActivity
().
getSharedPreferences
(
PLUGIN_NAME
,
Context
.
MODE_PRIVATE
).
edit
();
editor
.
clear
();
editor
.
commit
();
}
private
AlarmManager
getAlarmManager
()
{
private
static
AlarmManager
getAlarmManager
()
{
return
(
AlarmManager
)
cordova
.
getActivity
().
getSystemService
(
Context
.
ALARM_SERVICE
);
}
}
\ No newline at end of file
src/android/LocalNotificationRestore.java
0 → 100644
View file @
89015b59
/**
* LocalNotificationRestore.java
* Cordova LocalNotification Plugin
*
* Created by Sebastian Katzer (github.com/katzer) on 31/08/2013.
* Copyright 2013 Sebastian Katzer. All rights reserved.
* GPL v2 licensed
*/
package
de
.
appplant
.
cordova
.
plugin
;
import
java.util.Set
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
android.content.BroadcastReceiver
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.SharedPreferences
;
/**
* This class is triggered upon reboot of the device. It needs to re-register
* the alarms with the AlarmManager since these alarms are lost in case of
* reboot.
*/
public
class
LocalNotificationRestore
extends
BroadcastReceiver
{
@Override
public
void
onReceive
(
Context
context
,
Intent
intent
)
{
String
pluginName
=
LocalNotification
.
PLUGIN_NAME
;
if
(
LocalNotification
.
cordova
==
null
)
{
return
;
}
// Obtain alarm details form Shared Preferences
SharedPreferences
alarms
=
context
.
getSharedPreferences
(
pluginName
,
Context
.
MODE_PRIVATE
);
Set
<
String
>
alarmIds
=
alarms
.
getAll
().
keySet
();
/*
* For each alarm, parse its alarm options and register is again with
* the Alarm Manager
*/
for
(
String
alarmId
:
alarmIds
)
{
LocalNotificationOptions
options
;
JSONObject
args
;
try
{
args
=
new
JSONObject
(
alarms
.
getString
(
alarmId
,
""
));
options
=
new
LocalNotificationOptions
(
args
);
LocalNotification
.
add
(
options
);
}
catch
(
JSONException
e
)
{}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment