/
NotificationManagerModule.java
182 lines (159 loc) Β· 6.01 KB
/
NotificationManagerModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2017 by Axway, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.android.notificationmanager;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.common.Log;
import ti.modules.titanium.android.AndroidModule;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.pm.PackageManager;
import android.Manifest;
import android.os.Build;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.support.v4.app.NotificationManagerCompat;
import java.util.HashMap;
@Kroll.module(parentModule = AndroidModule.class)
public class NotificationManagerModule extends KrollModule
{
private static final String TAG = "TiNotification";
protected static final int PENDING_INTENT_FOR_ACTIVITY = 0;
protected static final int PENDING_INTENT_FOR_SERVICE = 1;
protected static final int PENDING_INTENT_FOR_BROADCAST = 2;
protected static final int PENDING_INTENT_MAX_VALUE = PENDING_INTENT_FOR_SERVICE;
private static NotificationManager notificationManager = null;
private static NotificationChannel defaultChannel = null;
public static final String DEFAULT_CHANNEL_ID = "ti_default_channel";
@Kroll.constant
public static final int DEFAULT_ALL = Notification.DEFAULT_ALL;
@Kroll.constant
public static final int DEFAULT_LIGHTS = Notification.DEFAULT_LIGHTS;
@Kroll.constant
public static final int DEFAULT_SOUND = Notification.DEFAULT_SOUND;
@Kroll.constant
public static final int DEFAULT_VIBRATE = Notification.DEFAULT_VIBRATE;
@Kroll.constant
public static final int FLAG_AUTO_CANCEL = Notification.FLAG_AUTO_CANCEL;
@Kroll.constant
public static final int FLAG_INSISTENT = Notification.FLAG_INSISTENT;
@Kroll.constant
public static final int FLAG_NO_CLEAR = Notification.FLAG_NO_CLEAR;
@Kroll.constant
public static final int FLAG_ONGOING_EVENT = Notification.FLAG_ONGOING_EVENT;
@Kroll.constant
public static final int FLAG_ONLY_ALERT_ONCE = Notification.FLAG_ONLY_ALERT_ONCE;
@Kroll.constant
public static final int FLAG_SHOW_LIGHTS = Notification.FLAG_SHOW_LIGHTS;
@SuppressWarnings("deprecation")
@Kroll.constant
public static final int STREAM_DEFAULT = Notification.STREAM_DEFAULT;
public NotificationManagerModule()
{
super();
}
// Kept for compatibility with 1.5.x
public NotificationProxy createNotification(Object[] args)
{
NotificationProxy notification = new NotificationProxy();
notification.handleCreationArgs(this, args);
return notification;
}
public static NotificationManager getManager()
{
if (notificationManager == null) {
notificationManager =
(NotificationManager) TiApplication.getInstance().getSystemService(Activity.NOTIFICATION_SERVICE);
}
return notificationManager;
}
public static boolean useDefaultChannel()
{
// use default channel if we are targeting API 26+
boolean useDefaultChannel =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& TiApplication.getInstance().getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O;
// setup default channel it it does not exist
if (useDefaultChannel && defaultChannel == null) {
defaultChannel =
new NotificationChannel(DEFAULT_CHANNEL_ID, "miscellaneous", NotificationManager.IMPORTANCE_DEFAULT);
getManager().createNotificationChannel(defaultChannel);
Log.w(
TAG,
"Falling back to default notification channel.\nIt is highly advised to create your own notification channel using Ti.Android.NotificationManager.createNotificationChannel()");
}
return useDefaultChannel;
}
@TargetApi(26)
@Kroll.method
public NotificationChannelProxy createNotificationChannel(Object[] args)
{
NotificationChannelProxy notificationChannelProxy = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannelProxy = new NotificationChannelProxy();
notificationChannelProxy.handleCreationArgs(this, args);
getManager().createNotificationChannel(notificationChannelProxy.getNotificationChannel());
}
return notificationChannelProxy;
}
@Kroll.method
public void cancel(int id)
{
getManager().cancel(id);
}
@Kroll.method
public void cancelAll()
{
getManager().cancelAll();
}
@Kroll.method
public void notify(int id, NotificationProxy notificationProxy)
{
Notification notification = notificationProxy.buildNotification();
// targeting Android O or above? create default channel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notification.getChannelId() == DEFAULT_CHANNEL_ID) {
useDefaultChannel();
}
getManager().notify(id, notification);
HashMap wakeParams = notificationProxy.getWakeParams();
if (wakeParams != null) {
int wakeTime = TiConvert.toInt(wakeParams.get("time"), 3000);
int wakeFlags = TiConvert.toInt(
wakeParams.get("flags"),
(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE));
TiApplication app = TiApplication.getInstance();
if (app.checkCallingOrSelfPermission(Manifest.permission.WAKE_LOCK) == PackageManager.PERMISSION_GRANTED) {
PowerManager pm = (PowerManager) app.getSystemService(TiApplication.POWER_SERVICE);
if (pm != null && !pm.isScreenOn()) {
try {
WakeLock wl = pm.newWakeLock(wakeFlags, "TiWakeLock");
wl.acquire(wakeTime);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
}
}
}
@Kroll.method
public boolean areNotificationsEnabled()
{
return NotificationManagerCompat.from(TiApplication.getInstance()).areNotificationsEnabled();
}
@Override
public String getApiName()
{
return "Ti.Android.NotificationManager";
}
}