-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPUSHNotificationWithScheduleActivity.java
More file actions
127 lines (112 loc) · 5.32 KB
/
PUSHNotificationWithScheduleActivity.java
File metadata and controls
127 lines (112 loc) · 5.32 KB
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
package com.example.myapplication;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
//<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
public class PUSHNotificationWithScheduleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_p_u_s_h_notification_with_schedule);
startTimer();
}
//Code for PUSH notifications
NotificationManager notificationManager = null;
public void createNotification(String title, String body) {
final int NOTIFY_ID = 1000;
// There are hardcoding only for show it's just strings
String name = "my_package_channel";
String id = "my_package_channel_1"; // The user-visible name of the channel.
String description = "my_package_first_channel"; // The user-visible description of the channel.
Intent intent;
PendingIntent pendingIntent;
NotificationCompat.Builder builder;
if (notificationManager == null) {
notificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
}
//Code for current versions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = notificationManager.getNotificationChannel(id);
if (mChannel == null) {
mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
mChannel.enableVibration(true);
mChannel.setLightColor(Color.GREEN);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(mChannel);
}
builder = new NotificationCompat.Builder(this, id);
intent = new Intent(this, PUSHNotificationWithScheduleActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentTitle(title) // required
.setSmallIcon(android.R.drawable.ic_popup_reminder) // required
.setContentText(body) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setTicker(title)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
} else {
//code for older versions
builder = new NotificationCompat.Builder(this);
intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentTitle(title) // required
.setSmallIcon(android.R.drawable.ic_popup_reminder) // required
.setContentText(body) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setTicker(title)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setPriority(Notification.PRIORITY_HIGH);
}
Notification notification = builder.build();
notificationManager.notify(NOTIFY_ID, notification);
}
//code for scheduling notifications
Timer timer;
TimerTask timerTask;
final Handler handler = new Handler();
public void startTimer() {
timer = new Timer();
initializeTimerTask();
timer.schedule(timerTask, 5000, 10000); //
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
double random = Math.random();
final String body = "There is a important value selected.";
if(random > 0.5) { //Here comes your custom logic
createNotification("PUSH Notification", body);
TextView tv = findViewById(R.id.textViewRandomValue);
tv.setText(String.valueOf(random));
}
}
});
}
};
}
}