-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotificationDisplayService.java
More file actions
54 lines (45 loc) · 2.28 KB
/
NotificationDisplayService.java
File metadata and controls
54 lines (45 loc) · 2.28 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
package de.derandroidpro.notificationtutorial2016;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
public class NotificationDisplayService extends Service {
final int NOTIFICATION_ID = 16;
public NotificationDisplayService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
displayNotification("Test-Notification", "Dies ist eine Test-Notification mit einem seeehr langen Text, den man ausklappen muss.");
stopSelf(); // Beendet den Service nach dem Ausführen des Codes (nachträglich ergänzt)
return super.onStartCommand(intent, flags, startId);
}
private void displayNotification(String title, String text){
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.ic_android_white_36dp)
//.setLargeIcon(BitmapFactory.decodeResource(R.drawable.xyz))
.setColor(getResources().getColor(R.color.colorAccent))
.setVibrate(new long[]{0, 300, 300, 300})
//.setSound()
.setLights(Color.WHITE, 1000, 5000)
//.setWhen(System.currentTimeMillis())
.setContentIntent(notificationPendingIntent)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setStyle(new NotificationCompat.BigTextStyle().bigText(text));
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification.build());
}
}