How do I run a notification when I trigger the alarm?

0

I have my class MainActivty and ReceberAlarm , class receberAlarm is the class that should trigger the notification, I ask for help.

public class MainActivity extends ActionBarActivity {
static final int NUM_ITEMS = 3;

GPSTracker gps;
public Double latitude;
public Double longitude;
MyAdapter mAdapter;

ViewPager mPager;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_pager);
    setUpPagerAndTabs();

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, 2015);
    calendar.set(Calendar.MONTH, 5);
    calendar.set(Calendar.DAY_OF_MONTH, 22);
    //calendar.set(Calendar.HOUR_OF_DAY,17); //Indicar horas
    //calendar.set(Calendar.MINUTE,35);//Indicar Minutos

    Intent intent = new Intent(MainActivity.this, ReceberAlarm.class);
    PendingIntent pIntent = PendingIntent.getBroadcast(MainActivity.this,0, intent,0);

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

The class I received the alarm:

public class ReceberAlarm extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
      MainActivity m = new MainActivity();
      m.showNotification(); // Activar Notificação
  }
}
    
asked by anonymous 22.05.2015 / 02:39

1 answer

0

You can try as follows:

No onReceive you fire the notification.

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.icone)
        .setContentTitle("Título")
        .setContentText("Mensagem");

int mNotificationId = 001;
NotificationManager mNotifyMgr = 
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
    
22.05.2015 / 12:59