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
Related
I want to just Generate a notification after save Video.
for this i'll try below code :
public void stopRecordingVideo() {
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
values.put(MediaStore.MediaColumns.DATA, file.toString());
activity.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
Toast.makeText(activity, "Video saved: " + file, Toast.LENGTH_SHORT).show();
//Log.v(TAG, "Video saved: " + getVideoFile(context));
Log.v(TAG, "Video saved: " + file);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(activity)
.setSmallIcon(R.drawable.ic_launcher_background) // notification icon
.setContentTitle("Notification!") // title for notification
.setContentText("Hello word") // message for notification
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(activity, RecorderActivity.class);
PendingIntent pi = PendingIntent.getActivity(activity,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
} else{
// do something for phones running an SDK before lollipop
}
try{
mMediaRecorder.stop();
mMediaRecorder.release();
}catch(RuntimeException stopException){
//handle cleanup here
}
try {
mSession.stopRepeating();
} catch (CameraAccessException e) {
e.printStackTrace();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
setup2Record();
} else {
finish(); //just blows up anyway.
}
}
Video save perfectly in Storage and Toast msg also show. but only notification not generated please help for this solution.
Solution:
On Android 8.0 (Oreo) you must have something called as a NotificationChannel So try the below implementation:
Step1: Create Notification Channel
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
Finally: Then your Notification:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(activity)
.setSmallIcon(R.drawable.ic_launcher_background) // notification icon
.setContentTitle("Notification!") // title for notification
.setContentText("Hello word") // message for notification
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(activity, RecorderActivity.class);
PendingIntent pi = PendingIntent.getActivity(activity,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
notificationManager.notify(1, mBuilder.build());
Try this, Hope it helps.
I have some problem in implement notification with android qiscus sdk.
I create class FirebaseService extend FirebaseMessagingService.
however, i cann't recived message in onMessageReceived when someone chat me. anyone can help me please ? thank you.
You have to extend with QiscusFirebaseService and set a remoteMessege to handleMessageReceived.
QiscusFirebaseService.handleMessageReceived(remoteMessage)
for detail example
https://gist.github.com/fauzisho/64b89df651c91b1dd6d4d096c8d826ca
i hope its can help you to solve your problem
try {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String title = "" + "";
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (defaultSound == null) {
defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
if (defaultSound == null) {
defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
}
}
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(intent)
.setSmallIcon(icon)
.setLights(Color.BLUE, 20, 80)
.setAutoCancel(true)
.setSound(defaultSound);
if (bitmap != null) {
builder.setLargeIcon(bitmap);
}
Notification not = new Notification.BigTextStyle(builder).bigText(message).build();
if (defaultSound == null) {
not.defaults |= Notification.DEFAULT_VIBRATE;
not.defaults |= Notification.DEFAULT_SOUND;
}
notificationManager.notify(0, not);
}
catch (Exception e) {
e.printStackTrace();
}`
I tried to put PendingIntent Notification in my android project but i had this error it says wrong first argument found android view.View.OnClickListener required android content,context.
Am going to show the code error , the "this" under else notification has its error.
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this)
this error
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int targetId = view.getId();
if (targetId == R.id.button) {
Toast.makeText(getApplicationContext(), "You have " + numMessages++ + " message", Toast.LENGTH_SHORT).show();
} else {
if (numMessages > 0) {
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this)
.setTicker("You Have " + numMessages++ + " message")
.setContentTitle("OneDiver")
.setContentText("Content")
.setSmallIcon(R.drawable.ic_od_icon_24dp)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
}
}
If this code is in an activity, say YourActivity.class, you need to replace "this" with "YourActivity.this". Because in an OnClickListener, "this" is referring to the OnClickListener instead of the activity.
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.
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!