Auto push notification shows after 24 hours in android studio programmatically




Lets start:

1. First of all you will create class MyNewIntentService
public class MyNewIntentService extends IntentService {
 private static final int NOTIFICATION_ID = 3;
 String title = "Title";
 String message = "show message";
public MyNewIntentService() {
super("MyNewIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {

NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("2",
"Title",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Description");
mNotificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), "2")
.setSmallIcon(R.drawable.app_icon) // notification icon
.setContentTitle(title) // title for notification
.setContentText(message)// message for notification
// .setTimeoutAfter(60000)// time notification
.setAutoCancel(true); // clear notification after click
intent = new Intent(getApplicationContext(), QuizCategoriesActivity.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pi);
mNotificationManager.notify(0, mBuilder.build());
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2. Create another class MyReceiver

public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}

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

Intent intent1 = new Intent(context, MyNewIntentService.class);
context.startService(intent1);
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

3. Open Manefist file

add this some line of code with in <application> ..... </application>
<receiver
    android:name=".notifi.MyReceiver"
android:enabled="true"
android:exported="false" >
</receiver>

<!--For Quiz Stages notification-->
<service
android:name=".notifi.MyNewIntentService"
android:exported="false" >
</ service>

4. Now open MainActivity class
Intent notifyIntent = new Intent(this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast
(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
1000 * 60 * 60 * 24, pendingIntent); // for 24 hrs




Post a Comment

Previous Post Next Post