package com.example.notification; import android.annotation.TargetApi; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.app.TaskStackBuilder; import android.content.Context; import android.content.Intent; import android.os.Build; import android.support.v4.app.NotificationCompat; import com.example.pharmacy.frontEnd.NotificationClicked; /** * Created by pharmacy on 09/01/2018. */ public abstract class Notification extends Service{ protected NotificationManager nm; protected int snId = 0; @Override public void onCreate(){ nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); buildNotification(); } protected void buildNotification(){ NotificationCompat.Builder mBuilder; if(Build.VERSION.SDK_INT >= 26){ NotificationChannel nc = createNotificationChannel(); mBuilder = new NotificationCompat.Builder(this, nc.getId()); } else { mBuilder = new NotificationCompat.Builder(this, "sednc"); mBuilder = setParameters(mBuilder); } //Up to set Content Intent taken from developers page and must be refined or if works cited. // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(this, NotificationClicked.class); // The stack builder object will contain an artificial back stack for the // started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(NotificationClicked.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(resultPendingIntent); // mId allows you to update the notification later on. nm.notify(snId, mBuilder.build()); } @TargetApi(26) private NotificationChannel createNotificationChannel(){ NotificationChannel nc = new NotificationChannel("sednc", "sedentaryNotification", NotificationManager.IMPORTANCE_DEFAULT); nc = setChannelParameters(nc); return nc; } //TBC used to alter alert type - audio, visual, haptic etc. May need separate methods for these protected abstract NotificationCompat.Builder setParameters(NotificationCompat.Builder builder); protected abstract NotificationChannel setChannelParameters(NotificationChannel nc); }