Notification in Android not repeating - java

i want a repeating notification that repeats daily but This code i wrote in Main Activity on create but notification is not repeating next day, only work for one day.
AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), Notification_Receiver.class);
intent.putExtra("title", "Prayer Time Intimation");
intent.putExtra("msg", "Time of Isha is About to End");
intent.putExtra("NOTIFhh", calendar.get(Calendar.HOUR_OF_DAY));
intent.putExtra("NOTIFmm", calendar.get(Calendar.MINUTE));
int id = endishaid;
intent.putExtra("notifyid", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager1.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Alaram Manger class
public class Notification_Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
long[] pattern = {0, 100, 200, 300};
Notification notification = new NotificationCompat.Builder(context, CHANNEL_1_ID)
.setSmallIcon(android.R.drawable.arrow_up_float)
.setContentTitle(title)
.setContentText(msg)
.setStyle(new NotificationCompat.BigTextStyle()
.setBigContentTitle(title)
.setSummaryText(msg))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setSound(alarmSound)
.setVibrate(pattern)
.setColor(16753737)
.setLights(16753737, 300, 1000)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(createID(), notification);
}

Related

AlarmManager doesn't seem to be working

I'm trying to schedule a method to be called using AlarmManager, but it doesn't seem to be working. I've looked at other examples and theirs isn't working for me. So I'm thinking it's something from my code. Here's the AlarmManager code:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(SplashScreenActivity.this, KinectReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(SplashScreenActivity.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 25);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
And my broadcast receiver:
public class KinectReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Done", Toast.LENGTH_LONG).show();
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_notify)
.setContentTitle("Kinect")
.setColor(ContextCompat.getColor(context, R.color.colorAccent))
.setWhen(System.currentTimeMillis())
.setContentText("Your Kinects and Likes have been refilled. Now get to swiping")
.setAutoCancel(true)
.setSound(uri)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Your Kinects and Likes have been refilled. Now get to swiping"))
.setVibrate(new long[] { 100, 500, 100, 500, 100 });
Intent targetIntent = new Intent(context, MainActivity.class);
targetIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
targetIntent.putExtra("action", "main");
targetIntent.putExtra("id", "");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
}
Neither the Toast nor the Notification show.
The AlarmManager code is in the first thing that runs in my launcher activity, in case that helps. Thanks
Please register ur receiver in manifeast file like this
In application tag
<receiver
android:name=".KinectReceiver">
Second question is how to cancel alarm
Here is ur answer
Intent myIntent = new Intent(MainActivity.this, AlarmActivity.class);
pendingIntent = PendingIntent.getActivity(CellManageAddShowActivity.this,
id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent.cancel();
alarmManager.cancel(pendingIntent);
The main thing that you will need is:
1).Create pending intent with the same id and appropriate intent FLAG.
2).Cancel that pending intent.
3).Cancel the alarm using alarm manager.

How resume app after click notification with Java Android?

I create a notification and I want to resume app after click notification. Now when I click notification , a notification dissapear , I want to resume my app (when is minimalize).
public static void getSynchronizeNotification(Context context, DataResponse dataResponse) {
Bitmap Largeicon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_SYNCHRONIZE_ID);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
KeyguardManager km = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("IN");
kl.disableKeyguard();
PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "SMOK Komunal");
wl.acquire();
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setCategory(Notification.CATEGORY_PROMO)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Zmiany po synchronizacji...")
.setStyle(new NotificationCompat.BigTextStyle().bigText(buildNotificationAfterSynchronizeText(dataResponse)))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setLargeIcon(Largeicon)
.setContentIntent(pendingIntent)
.setVibrate(vibratePattern)
.setLights(Color.GREEN, 2000, 2000)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
notificationManager.notify(0, notificationBuilder.build());
// wl.release();
}
Edit this line
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
I do this and it works :
In MainAcitivity I have :
public static Context currentContext;
Next in all activity in onCreate and onResume and do this :
MainActivity.currentContext = this;
And next when I build a notification I do this :
Intent intent = new Intent(context, MainActivity.currentContext.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
This works;
<activity android:name=".YourActivity" android:launchMode="singleTop">

Send a notification with android at an interval

I'm trying to set a notification to appear after a certain interval. I expected this to work however nothing seems to happen when running the code on my phone other than at reciever being printed to the console and I can't seem to find out why. Any help would be greatly appreciated.
Here is the code used to set the alarm.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 10);
Intent intent = new Intent(getApplicationContext(), NotificationReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIn
Here is the notification receiver class
public class NotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("at reciever");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent repeatingIntent = new Intent(context, LoginActivity.class);
repeatingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeatingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setContentTitle("Please Leave at the cage")
.setContentText("It almost 5")
.setAutoCancel(true);
notificationManager.notify(100, builder.build());
}
}
Here are the relevant manifest lines
receiver android:name=".NotificationReciever"
uses-permission android:name="com.android.alarm.permission.SET_ALARM"
Basically you need to set a icon for each notifications.
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setContentTitle("Please Leave at the cage")
.setSmallIcon(R.drawable.crosshair); //crosshair is the icon choice in this example
.setContentText("It almost 5")
.setAutoCancel(true);

I am trying to generate notification on app,but i got error cannot resolve setLatestEventInfo() method

private void Notify(String notificationTitle, String notificationMessage){
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
#SuppressWarnings("deprecation")
Notification notification = new Notification(R.drawable.logob,"New Message", System.currentTimeMillis());
Intent notificationIntent = new Intent(this,NotificationView.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(foradmin.this, notificationTitle,notificationMessage, pendingIntent);
notificationManager.notify(9999, notification);
}
Trying to generate notification on app,but i got error cannot resolve setLatestEventInfo() method.
try like this. it works perfectly.Hope this helps.Thanks
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setAutoCancel(false);
builder.setTicker("this is ticker text");
builder.setContentTitle("WhatsApp Notification");
builder.setContentText("You have a new message");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setSubText("This is subtext..."); //API level 16
builder.setNumber(100);
builder.build();
myNotication = builder.getNotification();
manager.notify(11, myNotication);

Activity shown immediatly instead of showing a notification

I'm trying to use the AlarmManager to send a notification to the user later in the day, whether he is using the application or not.
I basically pasted what I saw here : http://developer.android.com/guide/topics/ui/notifiers/notifications.html, but obviously something went wrong because instead of showing a notification, the AndroidLauncher Activity is shown.
Here is my code :
public void planNotification() {
AlarmManager am = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, AndroidLauncher.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(AndroidLauncher.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
int currentApiVersion = android.os.Build.VERSION.SDK_INT;
if (currentApiVersion >= 19)
am.setWindow(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, 5000, resultPendingIntent);
else
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 86400000, resultPendingIntent);
}
I'm still new to Intents and PendingIntents, so it may be a dumb mistake.
~~~ ~~~ EDIT : Thank you for your help ! Now that I understood that I need a BroadcastReceiver, I can't get it to work. It looks like onReceive() is never called.
Here is my Main Activity :
#Override
public void planNotification() {
AlarmManager am = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
int currentApiVersion = android.os.Build.VERSION.SDK_INT;
if (currentApiVersion >= 19)
am.setWindow(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, 5000, alarmIntent);
else
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, alarmIntent);
}
And my BroadcastReceiver :
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("ON RECEIVE");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(context, AndroidLauncher.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(AndroidLauncher.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(AndroidLauncher.notifId, mBuilder.build());
}
}
The way you have it, the alarm manager is directly using your resultIntent.
You should move your notification creation code to a BroadcastReceiver:
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, AndroidLauncher.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(AndroidLauncher.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
}
}
And use the AlarmManager to call the BroadcastReceiver:
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
if (currentApiVersion >= 19)
am.setWindow(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, 5000, alarmIntent);
else
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 86400000, alarmIntent);

Categories

Resources