kaveer
11/12/2016 - 3:01 AM

Notification

Notification

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        OnBootCompleteNotification(context);

    }

    public void OnBootCompleteNotification(Context context){
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_logo_new)
                        .setContentTitle("VMS Notification")
                        .setContentText("VMS will run when boot complete");

        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
    }
}

//in androidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.avitah.vms">

    <!-- add line to allow app to run on boot up -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".VMS"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name="com.example.avitah.Activity.Login">
            <intent-filter>
                //cause this activity to start first
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.avitah.Activity.SignUp" />

        <!-- add receiver tag to allow android to display message/toast/notification in the java class .BootReceiver -->
        <receiver
            android:name="com.example.avitah.Notification.BootReceiver"
            android:enabled="true"
            android:exported="true"
            android:label="BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <!-- Add service tag to run the java class .MyService when the app is actually running a specific activity -->
        <service android:name="com.example.avitah.Notification.MyService">
            <intent-filter>
                <action android:name="com.example.avitah.vms.MyService" />
            </intent-filter>
        </service>

    </application>

</manifest>
 int notificationId =1;
 
  public int onStartCommand(Intent intent, int flags, int startId){

        //Notification(notifTitle , notifSubject);

        String dateNow = GetDateNow();
        String addedDate = AddDateToCurrentDate(GetDateNow(), 3);
        DBHandler DB = new DBHandler(this);

        insuranceList = DB.GetNotificationInsurance(dateNow, addedDate);

        if (insuranceList.size() > 0){
            for (TableInsurance.InsuranceNonStatic insurance:  insuranceList) {
                notifTitle = "Insurance " + insurance.insuranceName;
                notifSubject = insurance.insuranceName + " expires on : " + insurance.expiryDate;
                Notification(notifTitle , notifSubject);
            }
        }
    }
    
     public void Notification(String title, String subject){

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(getApplicationContext())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(title)
                        .setContentText(subject);

        NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(++notificationId, mBuilder.build()); //increament notification id will display notification in the insurance list foreach loop
    }
public class MyService extends Service {
    String notifTitle = "Notification Title";
    String notifSubject = "Notification Subject";
    ArrayList<TableInsurance.InsuranceNonStatic> insuranceList;
    int notificationId =1;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId){

        //Notification(notifTitle , notifSubject);

        String dateNow = GetDateNow();
        String addedDate = AddDateToCurrentDate(GetDateNow(), 3);
        DBHandler DB = new DBHandler(this);

        insuranceList = DB.GetNotificationInsurance(dateNow, addedDate);

        if (insuranceList.size() > 0){
            for (TableInsurance.InsuranceNonStatic insurance:  insuranceList) {
                notifTitle = "Insurance " + insurance.insuranceName;
                notifSubject = insurance.insuranceName + " expires on : " + insurance.expiryDate;
                Notification(notifTitle , notifSubject);
            }
        }

        Toast.makeText(this, "Welcome to VMS by Avitah", Toast.LENGTH_LONG).show();
        return START_STICKY;
        //return super.onStartCommand(intent, flags, startId);
    }

    public String GetDateNow(){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); //import java.text.SimpleDateFormat instead of android.icu.text.simpleDateFormat
        String currentDate = dateFormat.format(calendar.getTime());
        return currentDate;
    }

    public String AddDateToCurrentDate(String dateNow, int dayToAdd){
        String result = "2017-01-03";

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(sdf.parse(dateNow));
            c.add(Calendar.DATE, dayToAdd);

            sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date resultDate = new Date(c.getTimeInMillis());
            result = sdf.format(resultDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return result;
    }

    public void Notification(String title, String subject){

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(getApplicationContext())
                        .setSmallIcon(R.drawable.ic_logo_new)
                        .setContentTitle(title)
                        .setContentText(subject);

        NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(++notificationId, mBuilder.build());
    }

    @Override
    public void onDestroy() {
        // STOP YOUR TASKS
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}



//in activity where need to start service
public class Login extends AppCompatActivity {
    EditText email;
    EditText password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        startService(); //call the method to run the tag SERVICE in android manifest which will run the java class MyService
    }

    //call the method to run the tag SERVICE in android manifest which will run the java class MyService
    public void startService() {
        startService(new Intent(getBaseContext(), MyService.class));
    }
}


//in androidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.avitah.vms">

    <!-- add line to allow app to run on boot up -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".VMS"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name="com.example.avitah.Activity.Login">
            <intent-filter>
                //cause this activity to start first
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.avitah.Activity.SignUp" />

        <!-- add receiver tag to allow android to display message/toast/notification in the java class .BootReceiver -->
        <receiver
            android:name="com.example.avitah.Notification.BootReceiver"
            android:enabled="true"
            android:exported="true"
            android:label="BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <!-- Add service tag to run the java class .MyService when the app is actually running a specific activity -->
        <service android:name="com.example.avitah.Notification.MyService">
            <intent-filter>
                <action android:name="com.example.avitah.vms.MyService" />
            </intent-filter>
        </service>

    </application>

</manifest>
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void SetNotification(){

        Notification n  = new Notification.Builder(this)
                .setContentTitle("New mail from " + "test@gmail.com")
                .setContentText("Subject")
                .setSmallIcon(R.drawable.ic_logo_new)
                .build();


        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(0, n);
    }