This repository has been archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotifier.java
77 lines (63 loc) · 2.3 KB
/
Notifier.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
package fr.julienmarcou.smartnotifier;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.support.v4.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.graphics.Color;
public class Notifier extends CordovaPlugin {
private int currentNotificationId = 1;
private int ledColor = Color.rgb(60, 255, 160);
private int ledTimeOn = 800;
private int ledTimeOff = 400;
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
}
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
JSONObject result = new JSONObject();
if(action.equals("notify")) {
this.notify(args.getString(0), args.getString(1), args.getString(2));
callbackContext.success(result);
return true;
}
return false;
}
private int getDrawableId(String resource) {
return this.cordova.getActivity().getResources().getIdentifier(
resource,
"drawable",
cordova.getActivity().getPackageName()
);
}
private void notify(String title, String message, String icon) {
// Notification content
NotificationCompat.Builder builder = new NotificationCompat.Builder(this.cordova.getActivity().getApplicationContext());
builder.setAutoCancel(true);
builder.setContentTitle(title);
builder.setContentText(message);
builder.setSmallIcon(this.getDrawableId(icon));
builder.setLights(ledColor, ledTimeOn, ledTimeOff);
// Notification behavior
builder.setContentIntent(PendingIntent.getActivity(
this.cordova.getActivity().getApplicationContext(),
0,
this.cordova.getActivity().getIntent(),
PendingIntent.FLAG_ONE_SHOT
));
// Notification building
Notification notif = builder.build();
// Notification issue
NotificationManager notifier = (NotificationManager) this.cordova.getActivity().getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notifier.notify(currentNotificationId, notif);
//currentNotificationId++;
}
}