Notification at a certain time with no need to open the program

Asked

Viewed 535 times

3

package com.example.dell.notification;
import android.annotation.TargetApi;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.format.Time;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.Toast;


import java.text.SimpleDateFormat;
import java.util.Date;


public class MainActivity extends ActionBarActivity {  

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

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
   String currentDateandTime = sdf.format(new Date());

if(currentDateandTime=="2015-05-15"){

}
            showNotification();


}


@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void showNotification(){

    // define sound URI, the sound to be played when there's a notification
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    // intent triggered, you can add other intent for other actions
    Intent intent = new Intent(MainActivity.this, NotificationReceiver.class);
    PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);

    // this is it, we'll build the notification!
    // in the addAction method, if you don't want any icon, just set the first param to 0
    Notification mNotification = new Notification.Builder(this)

            .setContentTitle("Vigilia dos Obreiros")
            .setContentText("Hoje, as 22 horas")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent)
            .setSound(soundUri)

            .addAction(R.drawable.abc_ic_ab_back_mtrl_am_alpha, "View", pIntent)
            .addAction(0, "Remind", pIntent)

            .build();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // If you want to hide the notification after it was selected, do the code below
    // myNotification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, mNotification);
}

public void cancelNotification(int notificationId){

    if (Context.NOTIFICATION_SERVICE!=null) {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager nMgr = (NotificationManager) getApplicationContext().getSystemService(ns);
        nMgr.cancel(notificationId);
    }
}
}

1 answer

3

Define a Alarmmanager to call a Broadcastreceiver which will launch the notification:

//Definir a data
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2015);
calendar.set(Calendar.MONTH, 5);
calendar.set(Calendar.DAY, 15);
//calendar.set(Calendar.HOUR_OF_DAY,10) //Se quiser indicar a hora
//calendar.set(Calendar.MINUTE,25)//Se quiser indicar os minutos
Intent intent = new Intent(MainActivity.this, LancaNotificacoaReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(MainActivity.this,0, intent,0);

AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

//Definir o alarme para acontecer no dia determinado
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);  

Define the Broadcastreceiver LancaNotificacoaReceiver and in the method onReceive create the notification:

public class LancaNotificacoaReceiver extends BroadcastReceiver {

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

        //      código da notificação
    }

}  

Don’t forget to register Broadcastreceiver on Androidmanifest.xml

<receiver android:name="a_sua_package.LancaNotificacoaReceiver"/>
  • I implemented the code, but the alarm is not going off, with everything I could understand the way to follow to have my solution. Thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.