Newer
Older

Peter Joseph De Jonckheere CESM2014
committed
package com.example.notification;

Peter Joseph De Jonckheere CESM2014
committed
import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.TaskStackBuilder;

Peter Joseph De Jonckheere CESM2014
committed
import android.content.Context;

Peter Joseph De Jonckheere CESM2014
committed
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import com.example.pharmacy.frontEnd.MainActivity;

Peter Joseph De Jonckheere CESM2014
committed
/**
* Created by pharmacy on 09/01/2018.
*/

Peter Joseph De Jonckheere CESM2014
committed
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, "sednc");
} else {
mBuilder = new NotificationCompat.Builder(this, "sednc");

Peter Joseph De Jonckheere CESM2014
committed
setParameters(mBuilder);

Peter Joseph De Jonckheere CESM2014
committed
}

Peter Joseph De Jonckheere CESM2014
committed

Peter Joseph De Jonckheere CESM2014
committed
//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, MainActivity.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(MainActivity.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);

Peter Joseph De Jonckheere CESM2014
committed
setChannelParameters(nc);

Peter Joseph De Jonckheere CESM2014
committed
return nc;
}

Peter Joseph De Jonckheere CESM2014
committed
//TBC used to alter alert type - audio, visual, haptic etc. May need separate methods for these

Peter Joseph De Jonckheere CESM2014
committed
protected abstract NotificationCompat.Builder setParameters(NotificationCompat.Builder builder);

Peter Joseph De Jonckheere CESM2014
committed

Peter Joseph De Jonckheere CESM2014
committed
protected abstract NotificationChannel setChannelParameters(NotificationChannel nc);