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
b539019b
Commit
b539019b
authored
Oct 06, 2013
by
Sebastián Katzer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Code rework
parent
787f4960
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
66 additions
and
24 deletions
+66
-24
LocalNotificationReceiver.java
src/android/LocalNotificationReceiver.java
+66
-24
No files found.
src/android/LocalNotificationReceiver.java
View file @
b539019b
...
@@ -14,7 +14,7 @@ import java.util.Calendar;
...
@@ -14,7 +14,7 @@ import java.util.Calendar;
import
org.json.JSONException
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
org.json.JSONObject
;
import
android.annotation.
TargetApi
;
import
android.annotation.
SuppressLint
;
import
android.app.ActivityManager
;
import
android.app.ActivityManager
;
import
android.app.Notification
;
import
android.app.Notification
;
import
android.app.Notification.Builder
;
import
android.app.Notification.Builder
;
...
@@ -32,17 +32,17 @@ import android.os.Bundle;
...
@@ -32,17 +32,17 @@ import android.os.Bundle;
* Android notification bar. The notification uses the default notification
* Android notification bar. The notification uses the default notification
* sound and it vibrates the phone.
* sound and it vibrates the phone.
*/
*/
@TargetApi
(
Build
.
VERSION_CODES
.
JELLY_BEAN
)
public
class
LocalNotificationReceiver
extends
BroadcastReceiver
{
public
class
LocalNotificationReceiver
extends
BroadcastReceiver
{
public
static
final
String
OPTIONS
=
"LOCAL_NOTIFICATION_OPTIONS"
;
public
final
String
OPTIONS
=
"LOCAL_NOTIFICATION_OPTIONS"
;
private
Context
context
;
private
LocalNotificationOptions
options
;
@Override
@Override
public
void
onReceive
(
Context
context
,
Intent
intent
)
{
public
void
onReceive
(
Context
context
,
Intent
intent
)
{
NotificationManager
notificationMgr
=
(
NotificationManager
)
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
LocalNotificationOptions
options
=
null
;
LocalNotificationOptions
options
=
null
;
Bundle
bundle
=
intent
.
getExtras
();
Bundle
bundle
=
intent
.
getExtras
();
int
id
=
0
;
JSONObject
args
;
JSONObject
args
;
try
{
try
{
...
@@ -50,9 +50,24 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
...
@@ -50,9 +50,24 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
options
=
new
LocalNotificationOptions
(
args
);
options
=
new
LocalNotificationOptions
(
args
);
}
catch
(
JSONException
e
)
{}
}
catch
(
JSONException
e
)
{}
try
{
this
.
context
=
context
;
id
=
Integer
.
parseInt
(
options
.
getId
());
this
.
options
=
options
;
}
catch
(
Exception
e
)
{}
if
(
options
.
getInterval
()
==
0
)
{
LocalNotification
.
unpersist
(
options
.
getId
());
}
else
if
(
isFirstAlarmInFuture
())
{
return
;
}
Builder
notification
=
buildNotification
();
if
(!
isInBackground
(
context
))
{
// exec foreground callback
invokeForegroundCallback
(
options
);
}
showNotification
(
notification
);
}
/*
/*
* If you set a repeating alarm at 11:00 in the morning and it
* If you set a repeating alarm at 11:00 in the morning and it
...
@@ -61,6 +76,7 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
...
@@ -61,6 +76,7 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
* 'forgotten' reminder for that day. Therefore we ignore the event
* 'forgotten' reminder for that day. Therefore we ignore the event
* if Android tries to 'catch up'.
* if Android tries to 'catch up'.
*/
*/
private
Boolean
isFirstAlarmInFuture
()
{
if
(
options
.
getInterval
()
>
0
)
{
if
(
options
.
getInterval
()
>
0
)
{
Calendar
now
=
Calendar
.
getInstance
();
Calendar
now
=
Calendar
.
getInstance
();
Calendar
alarm
=
options
.
getCalendar
();
Calendar
alarm
=
options
.
getCalendar
();
...
@@ -71,21 +87,21 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
...
@@ -71,21 +87,21 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
int
currentMin
=
now
.
get
(
Calendar
.
MINUTE
);
int
currentMin
=
now
.
get
(
Calendar
.
MINUTE
);
if
(
currentHour
!=
alarmHour
&&
currentMin
!=
alarmMin
)
{
if
(
currentHour
!=
alarmHour
&&
currentMin
!=
alarmMin
)
{
return
;
return
true
;
}
}
}
}
else
{
LocalNotification
.
unpersist
(
options
.
getId
());
};
String
packageName
=
context
.
getPackageName
();
return
false
;
Intent
launchIntent
=
context
.
getPackageManager
().
getLaunchIntentForPackage
(
packageName
);
}
PendingIntent
contentIntent
=
PendingIntent
.
getActivity
(
context
,
0
,
launchIntent
,
PendingIntent
.
FLAG_UPDATE_CURRENT
);
/**
* Erstellt die Notification.
*/
private
Builder
buildNotification
()
{
Builder
notification
=
new
Notification
.
Builder
(
context
)
Builder
notification
=
new
Notification
.
Builder
(
context
)
.
setContentTitle
(
options
.
getTitle
())
.
setContentTitle
(
options
.
getTitle
())
.
setContentText
(
options
.
getSubTitle
())
.
setContentText
(
options
.
getSubTitle
())
.
setNumber
(
options
.
getBadge
())
.
setNumber
(
options
.
getBadge
())
.
setContentIntent
(
contentIntent
)
.
setTicker
(
options
.
getTitle
())
.
setTicker
(
options
.
getTitle
())
.
setSmallIcon
(
options
.
getIcon
());
.
setSmallIcon
(
options
.
getIcon
());
...
@@ -93,23 +109,49 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
...
@@ -93,23 +109,49 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
if
(
isInBackground
(
context
))
{
if
(
isInBackground
(
context
))
{
// app is in background
// app is in background
notification
.
setDefaults
(
Notification
.
DEFAULT_SOUND
);
notification
.
setDefaults
(
Notification
.
DEFAULT_SOUND
);
}
else
{
// exec foreground callback
invokeForegroundCallback
(
options
);
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
// missing GET_TASKS permission
// missing GET_TASKS permission
notification
.
setDefaults
(
Notification
.
DEFAULT_SOUND
);
notification
.
setDefaults
(
Notification
.
DEFAULT_SOUND
);
}
}
/*
setClickEvent
(
notification
);
* If you want all reminders to stay in the notification bar, you should
* generate a random ID. If you want do replace an existing
return
notification
;
* notification, make sure the ID below matches the ID that you store in
}
* the alarm intent.
/**
* Fügt der Notification einen onclick Handler hinzu.
*/
private
Builder
setClickEvent
(
Builder
notification
)
{
String
packageName
=
context
.
getPackageName
();
Intent
launchIntent
=
context
.
getPackageManager
().
getLaunchIntentForPackage
(
packageName
);
PendingIntent
contentIntent
=
PendingIntent
.
getActivity
(
context
,
0
,
launchIntent
,
PendingIntent
.
FLAG_UPDATE_CURRENT
);
return
notification
.
setContentIntent
(
contentIntent
);
}
/**
* Zeigt die Notification an.
*/
*/
@SuppressWarnings
(
"deprecation"
)
@SuppressLint
(
"NewApi"
)
private
void
showNotification
(
Builder
notification
)
{
NotificationManager
notificationMgr
=
(
NotificationManager
)
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
int
id
=
0
;
try
{
id
=
Integer
.
parseInt
(
options
.
getId
());
}
catch
(
Exception
e
)
{}
if
(
Build
.
VERSION
.
SDK_INT
<
16
)
{
// build notification for HoneyComb to ICS
notificationMgr
.
notify
(
id
,
notification
.
getNotification
());
}
else
if
(
Build
.
VERSION
.
SDK_INT
>
15
)
{
// Notification for Jellybean and above
notificationMgr
.
notify
(
id
,
notification
.
build
());
notificationMgr
.
notify
(
id
,
notification
.
build
());
}
}
}
/**
/**
* Gibt an, ob die App im Hintergrund läuft.
* Gibt an, ob die App im Hintergrund läuft.
...
...
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