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
39b26a6d
Commit
39b26a6d
authored
Nov 26, 2013
by
Sebastián Katzer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert indentations to spaces.
parent
52df6198
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
133 additions
and
132 deletions
+133
-132
LocalNotificationReceiver.java
src/android/LocalNotificationReceiver.java
+133
-132
No files found.
src/android/LocalNotificationReceiver.java
View file @
39b26a6d
...
...
@@ -34,142 +34,142 @@ import android.os.Bundle;
*/
public
class
LocalNotificationReceiver
extends
BroadcastReceiver
{
public
static
final
String
OPTIONS
=
"LOCAL_NOTIFICATION_OPTIONS"
;
private
Context
context
;
private
LocalNotificationOptions
options
;
@Override
public
void
onReceive
(
Context
context
,
Intent
intent
)
{
LocalNotificationOptions
options
=
null
;
Bundle
bundle
=
intent
.
getExtras
();
JSONObject
args
;
try
{
args
=
new
JSONObject
(
bundle
.
getString
(
OPTIONS
));
options
=
new
LocalNotificationOptions
(
args
,
context
);
}
catch
(
JSONException
e
)
{}
this
.
context
=
context
;
this
.
options
=
options
;
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
* should trigger every morning at 08:00 o'clock, it will
* immediately fire. E.g. Android tries to make up for the
* 'forgotten' reminder for that day. Therefore we ignore the event
* if Android tries to 'catch up'.
*/
private
Boolean
isFirstAlarmInFuture
()
{
if
(
options
.
getInterval
()
>
0
)
{
Calendar
now
=
Calendar
.
getInstance
();
Calendar
alarm
=
options
.
getCalendar
();
int
alarmHour
=
alarm
.
get
(
Calendar
.
HOUR_OF_DAY
);
int
alarmMin
=
alarm
.
get
(
Calendar
.
MINUTE
);
int
currentHour
=
now
.
get
(
Calendar
.
HOUR_OF_DAY
);
int
currentMin
=
now
.
get
(
Calendar
.
MINUTE
);
if
(
currentHour
!=
alarmHour
&&
currentMin
!=
alarmMin
)
{
return
true
;
}
}
return
false
;
}
/**
* Erstellt die Notification.
*/
private
Builder
buildNotification
()
{
Builder
notification
=
new
Notification
.
Builder
(
context
)
.
setContentTitle
(
options
.
getTitle
())
.
setContentText
(
options
.
getMessage
())
.
setNumber
(
options
.
getBadge
())
.
setTicker
(
options
.
getTitle
())
.
setSmallIcon
(
options
.
getIcon
())
.
setSound
(
options
.
getSound
());
setClickEvent
(
notification
);
return
notification
;
}
/**
* 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
mgr
=
(
NotificationManager
)
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
int
id
=
0
;
try
{
id
=
Integer
.
parseInt
(
options
.
getId
());
}
catch
(
Exception
e
)
{}
public
static
final
String
OPTIONS
=
"LOCAL_NOTIFICATION_OPTIONS"
;
private
Context
context
;
private
LocalNotificationOptions
options
;
@Override
public
void
onReceive
(
Context
context
,
Intent
intent
)
{
LocalNotificationOptions
options
=
null
;
Bundle
bundle
=
intent
.
getExtras
();
JSONObject
args
;
try
{
args
=
new
JSONObject
(
bundle
.
getString
(
OPTIONS
));
options
=
new
LocalNotificationOptions
(
args
,
context
);
}
catch
(
JSONException
e
)
{}
this
.
context
=
context
;
this
.
options
=
options
;
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
* should trigger every morning at 08:00 o'clock, it will
* immediately fire. E.g. Android tries to make up for the
* 'forgotten' reminder for that day. Therefore we ignore the event
* if Android tries to 'catch up'.
*/
private
Boolean
isFirstAlarmInFuture
()
{
if
(
options
.
getInterval
()
>
0
)
{
Calendar
now
=
Calendar
.
getInstance
();
Calendar
alarm
=
options
.
getCalendar
();
int
alarmHour
=
alarm
.
get
(
Calendar
.
HOUR_OF_DAY
);
int
alarmMin
=
alarm
.
get
(
Calendar
.
MINUTE
);
int
currentHour
=
now
.
get
(
Calendar
.
HOUR_OF_DAY
);
int
currentMin
=
now
.
get
(
Calendar
.
MINUTE
);
if
(
currentHour
!=
alarmHour
&&
currentMin
!=
alarmMin
)
{
return
true
;
}
}
return
false
;
}
/**
* Erstellt die Notification.
*/
private
Builder
buildNotification
()
{
Builder
notification
=
new
Notification
.
Builder
(
context
)
.
setContentTitle
(
options
.
getTitle
())
.
setContentText
(
options
.
getMessage
())
.
setNumber
(
options
.
getBadge
())
.
setTicker
(
options
.
getTitle
())
.
setSmallIcon
(
options
.
getIcon
())
.
setSound
(
options
.
getSound
());
setClickEvent
(
notification
);
return
notification
;
}
/**
* 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
mgr
=
(
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
mgr
.
notify
(
id
,
notification
.
getNotification
());
mgr
.
notify
(
id
,
notification
.
getNotification
());
}
else
if
(
Build
.
VERSION
.
SDK_INT
>
15
)
{
// Notification for Jellybean and above
mgr
.
notify
(
id
,
notification
.
build
());
mgr
.
notify
(
id
,
notification
.
build
());
}
}
/**
* Gibt an, ob die App im Hintergrund läuft.
*/
private
boolean
isInBackground
(
Context
context
)
{
return
!
context
.
getPackageName
().
equalsIgnoreCase
(((
ActivityManager
)
context
.
getSystemService
(
Context
.
ACTIVITY_SERVICE
)).
getRunningTasks
(
1
).
get
(
0
).
topActivity
.
getPackageName
());
}
/**
* Ruft die `foreground` Callback Funktion auf.
*/
private
void
invokeForegroundCallback
(
LocalNotificationOptions
options
)
{
String
function
=
options
.
getForeground
();
if
(
function
!=
null
)
{
LocalNotification
.
webView
.
sendJavascript
(
function
+
"("
+
options
.
getId
()
+
")"
);
}
}
/**
* Ruft die `background` Callback Funktion auf.
*/
private
void
invokeBackgroundCallback
(
LocalNotificationOptions
options
)
{
String
function
=
options
.
getBackground
();
if
(
function
!=
null
)
{
LocalNotification
.
webView
.
sendJavascript
(
function
+
"("
+
options
.
getId
()
+
")"
);
}
}
/**
* Gibt an, ob die App im Hintergrund läuft.
*/
private
boolean
isInBackground
(
Context
context
)
{
return
!
context
.
getPackageName
().
equalsIgnoreCase
(((
ActivityManager
)
context
.
getSystemService
(
Context
.
ACTIVITY_SERVICE
)).
getRunningTasks
(
1
).
get
(
0
).
topActivity
.
getPackageName
());
}
/**
* Ruft die `foreground` Callback Funktion auf.
*/
private
void
invokeForegroundCallback
(
LocalNotificationOptions
options
)
{
String
function
=
options
.
getForeground
();
if
(
function
!=
null
)
{
LocalNotification
.
webView
.
sendJavascript
(
function
+
"("
+
options
.
getId
()
+
")"
);
}
}
/**
* Ruft die `background` Callback Funktion auf.
*/
private
void
invokeBackgroundCallback
(
LocalNotificationOptions
options
)
{
String
function
=
options
.
getBackground
();
if
(
function
!=
null
)
{
LocalNotification
.
webView
.
sendJavascript
(
function
+
"("
+
options
.
getId
()
+
")"
);
}
}
}
}
\ No newline at end of file
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