Notifications - Theorie!

StefMa

StefMa

Dauergast
450
Hi,

habe eben mal ein bisschen die Notifications ausgearbeitet.
Also möchte ich meine erkenntnisse auch mit euch teilen.
PHP:
// #1 First Source -> http://s.stefma.ws/153081
// #2 Second Source -> http://s.stefma.ws/de9c34 (Glaube das muss ich mir anschauen. Da wird utnerschieden, ob die
//                                                Activity aufgerufen wird, die generll im "Workflow" ist. Oder eine neue
//                                                die der User wirklich nur sieht, wenn er auf die notifiy klickt.
// #3 Third Source -> http://s.stefma.ws/916627
// #4 Fourth Source -> http://s.stefma.ws/f7b2b9

/* #1 --------------------------------------------------------------------------------- #1 */
NotificationCombat.Builder builder = new NotificationCombat.Builder(this);
// Required methods's
builder.setSmallIcon(R.drawable.icon);
buider.setContentTitle("Notification TITLE");
builder.setContentText("Content Text");
// Rest is optimal

// To define an action (onClick):
Intent resultIntent = new Intent(this, ActivityToStart.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURREN);
builder.setContentIntent(resultPendingIntent);

// We set an NotificationID because we must "update"
// the Notification later...
// To "notification" the System:
int notificationId = 222;
NotificationManager notifiMgr = (NotificationManager) getSystemServer(NOTIFICATION_SERVICE);
notifiMgr.notify(notificationId, builder.build());
/* #1 --------------------------------------------------------------------------------- #1 */

/* #2 --------------------------------------------------------------------------------- #2 */
/* #2 --------------------------------------------------------------------------------- #2 */

/* #3 --------------------------------------------------------------------------------- #3 */
// To update the current notification:
// Make the same like above. But with the same notifiacciton ID!!
// ....
builder = new NotificationCombat.Builder(this);
builder.setContentTitle("New Content Title");
// ....
// Here start of a loop that processes data and then notifies the user:
String newText = "New Notification";
for(int i = 0; i < 5; i+=1) {
   newText += "\nNochmal ich! " + i;
}
builder.setContentText(newText)
// setNumber is for the count of changes
builder.setNumber(5);

// Last step. Notifi the system with same ID
notifiManager.notifiy(222, builder.build());
/* #3 --------------------------------------------------------------------------------- #3 */

/* #4 --------------------------------------------------------------------------------- #4 */
// For the Big View e.g. for the addAction
// new need two or more Intens
Intent firstAction = new Intent(this, First.class);
firstAction.setAction("anyway"); // in the example is CommonConstatns.ACTION_DISSMISS
PendingIntent firstPending = PendingIntent.getService(this, 0 firstAction, 0);

Intent secondAction = new Intent(this, Second.class);
secondAction.setAction("anyway-2"); // in the example is CommonConstatns.ACTION_DISSMISS
PendingIntent secondPending = PendingIntent.getService(this, 0 secondAction, 0);

// Now we set the style of the notiffication
// and add the actions
builder.setStyle(new NotificationCompat.BigTextStyle().bigText("anyway :D"));
builder.addAction(R.drawable.firstAction, "String1", firstPending);
builder.addAction(R.drawable.secondAction, "String2", secondPending);
/* #4 --------------------------------------------------------------------------------- #4 */

Gruß
 
  • Danke
Reaktionen: Flocke123 und blackfire185

Ähnliche Themen

L
Antworten
3
Aufrufe
1.314
deek
D
S
  • Schneemann3
Antworten
0
Aufrufe
813
Schneemann3
S
X
Antworten
0
Aufrufe
1.184
xSession265
X
Zurück
Oben Unten