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!