0

I want to create a simple push notification for my Android app (repeating every day on 6pm)

I did some search and found: How To give notifications on android on specific time?

I followed that tutorial and here is what I got so far:

public class MyReceiver extends BroadcastReceiver
{
public MyReceiver()
{

}
@Override
public void onReceive(Context context, Intent intent)
{
    Intent intent1 = new Intent(context, HistoryToday.class);
    context.startService(intent1);
}
}


public class MyNewIntentService extends IntentService

{ private static final int NOTIFICATION_ID = 3;

public MyNewIntentService() {
    super("MyNewIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    Notification.Builder builder = new Notification.Builder(this);
    builder.setContentTitle("My Titel");
    builder.setContentText("This is the Body");
    builder.setSmallIcon(R.mipmap.logo_app_icon);
    Intent notifyIntent = new Intent(this, HistoryToday.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    //to be able to launch your activity from the notification
    builder.setContentIntent(pendingIntent);
    Notification notificationCompat = builder.build();
    NotificationManagerCompat managerCompat =  NotificationManagerCompat.from(this);
    managerCompat.notify(NOTIFICATION_ID, notificationCompat);
}

}

Manifest

<receiver
android:name=".MyReceiver"
android:enabled="true"
    android:exported="false" >
</receiver>
<service
    android:name=".MyNewIntentService"
    android:exported="false" >
</service>

Java class

    private void Intend()
{
    Context context = this.getApplicationContext();
    Intent notifyIntent = new Intent(this,MyReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast
            (context, 1, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,  System.currentTimeMillis(),
            10000, pendingIntent);
}

Sorry for the bad formatting, I hope it is readable

No notification is shown :-( Maybe someone can help me out. Thanks to all!

Michael F
  • 3
  • 2

1 Answers1

0

Try AlarmManager, it is the best option that I can suggest to you according to your requirements. Check this code:

Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar

 Calendar cal = new GregorianCalendar();
 cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
 cal.set(Calendar.HOUR_OF_DAY, 18);
 cal.set(Calendar.MINUTE, 00);
 cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
 cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
 cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
 cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
 Intent intent = new Intent(ProfileList.this, 
 IntentBroadcastedReceiver.class);
 PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0);
 AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
 alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);

This code will register the event everyday for 6PM. Now you have to catch the event by adding a broadcast receiver. Here is the code:

public class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {

// Generate a push notification here.

}
}

Don't forget to define the Broadcast Receiver in Manifest:

<receiver android:name=".MyBroadcastReceiver" >
Zohaib Hassan
  • 984
  • 2
  • 7
  • 11