How do I rebuilt my Notification to get a 'Notification Alarm'
I tried varois things but nothing worked, I am really frustated so I finally ask here.
Main Activity:
public void sendNotificationIfTimeEnd01() {
Context context = getApplicationContext();
Intent intent01 = new Intent(context, MainActivity.class);
PendingIntent pendingIntent01 = PendingIntent.getActivity(this, 1, intent01, 0);
NotificationCompat.Builder builder01 = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_notification)
.setContentIntent(pendingIntent01)
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setContentTitle(gamesJuliToStringArray[1])
.setContentText("Spiel ist bereit")
.setSubText("click");
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID_01, builder01.build());
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
Condition in my Fragment:
if (!notification062 && !buttonColor02.equals("red02")) {
activity.sendNotificationIfTimeEnd01();
}
Again I get Notifications if the conditions happens, but only inside of my application.
Need help for rebuilding this into and alarm notification
Here you can see an example of the AlarmManager being used to schedule a notification for a later time, given a specific delay.
Related
I want to know how can I know if the user dose not response to the notification?
I have yes and no actions , I want when the user press yes do nothing and when he click no will send sms message and when he did not response at all there will be a timer for 20 second when it finish it will also send sms message , everything work fine but the question is how can I know if the user didn't response at all to these actions?
in this code the timer start even if the user response which is not correct.
pleeeeas help I'm dying here!
Thank you,
public void notification(){
String title= getString(R.string.alarm);
//No
Intent activityIntent=new Intent(this, controllingScreen.class);
PendingIntent contentPendingIntent=PendingIntent.getActivity(this,0,activityIntent,0);
Intent actionActivityIntent=new Intent(this, SMS.class);
PendingIntent actionPendingIntent=PendingIntent.getActivity(
this,0,actionActivityIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//yes
Intent activityIntentYes=new Intent(this, controllingScreen.class);
PendingIntent contentPendingIntentYes=PendingIntent.getActivity(this,0,activityIntentYes,0);
Intent actionActivityIntentYes=new Intent(this, HomePage.class);
PendingIntent actionPendingIntentYes=PendingIntent.getActivity(
this,0,actionActivityIntentYes,PendingIntent.FLAG_UPDATE_CURRENT);
Vibrator v=(Vibrator) getSystemService(VIBRATOR_SERVICE);
NotificationManager manager;
manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(controllingScreen.this,App.CHANNEL_ONE_ID)
.setSmallIcon(R.drawable.icon)
.addAction(R.mipmap.ic_launcher_round,getString(R.string.yes) ,actionPendingIntentYes)
.addAction(R.mipmap.ic_launcher_round,getString(R.string.no) ,actionPendingIntent)
.setContentTitle(title)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setColor(Color.BLUE)
.setOnlyAlertOnce(true)
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.build();
v.vibrate((1000));
manager.notify(1,notification);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 20);
try {
Thread.sleep(20000);
Intent sms2 = new Intent(controllingScreen.this, SMS.class);
startActivity(sms2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
How can I create simple notification, which will close the application (in multitasking) after click? Thanks. (Java, Android)
first we should create notification that call an activity :
Intent intent = new Intent(this, YourClass.class);
intent.putExtra("NotiClick",true);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
Notification Noti;
Noti = new Notification.Builder(this)
.setContentTitle("YourTitle")
.setContentText("YourDescription")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, Noti);
}
Then in the onCreate() of your class do this:
Bundle extras = getIntent().getExtras();
if(extras == null)
{
//not being clicked on
}
else if (extras.getBoolean("NotiClick"))
{
//being clicked
}
and for completely close application you can kill process :
android.os.Process.killProcess(android.os.Process.myPid());
I built an notification which works so far.
Since I did not know that it works only inside of my application I read through some articles and saw that I need an 'Alarm Notification'.
I tried varous things but none did not work, now I am asking here.
My Notification in my Mainactivity looks like this:
public static final int NOTIFICATION_ID_01 = 1;
public void sendNotificationIfTimeEnd01() {
Context context = getApplicationContext();
Intent intent01 = new Intent(context, MainActivity.class);
PendingIntent pendingIntent01 = PendingIntent.getActivity(this, 1, intent01, 0);
NotificationCompat.Builder builder01 = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_notification)
.setContentIntent(pendingIntent01)
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setContentTitle(gamesJuliToStringArray[0])
.setContentText("Ready")
.setSubText("click");
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID_01, builder01.build());
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
I am accesing this notification in my Fragment if a specific conditions happens:
final MainActivity activity = (MainActivity) getActivity();
if (i = 10) {
activity.sendNotificationIfTimeEnd01();
editor.putBoolean("notification01", true);
editor.apply();
}
Again I want to transform this Notification into an 'Alarm' / 'Alarm Notification'.
I appreciate any response!
How do I sent the notification only once if something happens?
I got this statement:
if (diffDays <= 0 && diffHours <= 0 && diffMinutes <= 0) {
activity.sendNotificationIfTimeEnd01();
Log.d("MyApp", "I am here");
}
and this:
public void sendNotificationIfTimeEnd01() {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.google.de/?gws_rd=ssl"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_stat_notification);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
builder.setContentTitle("String one");
builder.setContentText("bla");
builder.setSubText("blabla");
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
I do get the notification if the statement is right, but if I close the app and start it I get the notification again.(Statement is still right);
Try using SharedPreferences as bleeding182 adviced you. This is a good answer on how to do that:
How to use SharedPreferences in Android to store, fetch and edit values
I'm trying to create a JB notification, but I can't make it show and it doesn't give any error in the logcat, just it doesn't do anything.
The code I'm using to create notification is the following
private void bigPictureNotification(){
Notification.Builder builder;
Notification notification;
NotificationManager manager;
Intent intent = new Intent(this, NotificationPendingIntent.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
//carica l'immagine
Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.androidworld);
builder = new Notification.Builder(getApplicationContext());
builder.setContentTitle("Titolo piccolo");
builder.setContentText("Testo della notifica");
builder.setSmallIcon(R.drawable.ic_stat_notifica);
builder.setLargeIcon(icon);
builder.addAction(R.drawable.ic_action_search, "Cerca", pendingIntent);
builder.addAction(R.drawable.ic_stat_notifica, "AndroidWorld", pendingIntent);
builder.addAction(R.drawable.ic_launcher, "Launcher", pendingIntent);
builder.build();
notification = new Notification.BigPictureStyle(builder).bigPicture(icon).build();
//notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, notification);
}
Where is the mistake?
Thanks for the help
Try and add builder.setContentIntent(pendingIntent); before builder.build(); Worked for me after I put that in.
I'd remove the call to
builder.build()
just before
notification = ...
as builder.build() gives you a notification object. Give that a shot.