Skip to content
Snippets Groups Projects
Notification.java 2.68 KiB
Newer Older
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.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

import com.example.pharmacy.frontEnd.MainActivity;

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");
            setParameters();
        }

        //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);
        setChannelParameters();
        return nc;
    }

    //TBC used to alter alert type - audio, visual, haptic etc. May need separate methods for these
    protected abstract void setParameters();

    protected abstract void setChannelParameters();