Skip to content
Snippets Groups Projects
SedentaryNotification.java 2.79 KiB
Newer Older
import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;


import com.example.pharmacy.frontEnd.MainActivity;
import com.example.pharmacy.frontEnd.R;

/**
 * Created by pharmacy on 09/01/2018.
 */

public class SedentaryNotification implements Notification {

    private int snId = 0;

    public boolean send(Context context) {
        buildNotification(context);
        return false;
    }

    private boolean buildNotification(Context context){

        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder;
        if(Build.VERSION.SDK_INT >= 26){
            NotificationChannel nc = createNotificationChannel();
            mBuilder = new NotificationCompat.Builder(context, "sednc");
        } else {
            mBuilder = new NotificationCompat.Builder(context, "sednc");
            //Set parameters for non nc Builder here
        }


        //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(context, 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(context);
// 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.
        mNotificationManager.notify(snId, mBuilder.build());
    @TargetApi(26)
    private NotificationChannel createNotificationChannel(){
        NotificationChannel nc = new NotificationChannel("sednc", "sedentaryNotification", NotificationManager.IMPORTANCE_DEFAULT);
        //set parameters of nc dependent on user requiements
        return nc;
    }