-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyNotification.java
73 lines (67 loc) · 3.65 KB
/
myNotification.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
package com.example.offline;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
public class myNotification {
private PendingIntent notificationPendingIntent;
/**
* This is the method called to create the Notification
*/
public android.app.Notification setNotification(Context context, String title, String text, int icon) {
if (notificationPendingIntent == null) {
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationPendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
}
android.app.Notification notification;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// OREO
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
CharSequence name = "Permanent Notification";
//mContext.getString(R.string.channel_name);
int importance = NotificationManager.IMPORTANCE_LOW;
String CHANNEL_ID = "com.example.offline.channel";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
//String description = mContext.getString(R.string.notifications_description);
String description = "I would like to receive travel alerts and notifications for:";
channel.setDescription(description);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
notification = notificationBuilder
.setSmallIcon(icon)
.setColor(ContextCompat.getColor(context, R.color.colorAccent))
.setContentTitle(title)
.setContentText(text)
.setContentIntent(notificationPendingIntent)
.build();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notification = new NotificationCompat.Builder(context, "channel")
// to be defined in the MainActivity of the app
.setSmallIcon(icon)
.setContentTitle(title)
// .setColor(context.getResources().getColor(R.color.colorAccent))
.setContentText(text)
.setPriority(android.app.Notification.PRIORITY_MIN)
.setContentIntent(notificationPendingIntent).build();
} else {
notification = new NotificationCompat.Builder(context, "channel")
// to be defined in the MainActivity of the app
.setSmallIcon(icon)
.setContentTitle(title)
.setContentText(text)
.setPriority(android.app.Notification.PRIORITY_MIN)
.setContentIntent(notificationPendingIntent).build();
}
return notification;
}
}