Respones Notification - java

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();
}
}

Related

How to disable FCM on spesific activity Android

i want to ask about FCM. I have an FCM service that is running as it should this is my code
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "ch1")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
NotificationChannel channel = new NotificationChannel("ch1",
getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.enableVibration(true);
mNotificationManager.createNotificationChannel(channel);
}
mNotificationManager.notify(0, notificationBuilder.build());
but i want to disable this notification on spesific activity, for example i have ChatActivity. where if i get notificatin on this activity, the fcm service is not showing a notification. I have tried implement this code but still not work
if(!(this.getApplicationContext() instanceof ChatActivity)){
//build the notification
}
anyone have solution for my problem? thank you.
First of all keep a flag in your SharedPreferences for ChatActivity to know if it's open or not.
Now inside ChatActivity set this flag true in onResume() and false in onStop(). Like this:
#Override
protected void onResume() {
super.onResume();
AppPreferences.getInstance().setBoolean("IS_CHAT_ACTIVITY", true);
}
#Override
protected void onStop() {
super.onStop();
AppPreferences.getInstance().setBoolean("IS_CHAT_ACTIVITY", true);
}
If you don't know anything about SharedPreferences check This Tutorial.
Now in your FCM Service class, you have onMessageReceived() callback, which is triggered every time a notification comes.
Now Do this in your onMessageReceived() callback:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(remoteMessage.getNotification() != null) {
if (AppPreferences.getInstance().getBoolean("IS_CHAT_ACTIVITY", false)) {
//Do Nothing, Ignore Notification
} else {
//Write code to show notification here
//Show Notification
generateNotification(remoteMessage.getNotification());
}
}
}
That's it, that's how you can prevent the notification while you’re on ChatActivity.

Reminder notification doesn't show up

I'm making an app to remind the user of something. I want to show a notification at some time in the future. I've written the code below, following some tutorials, but it doesn't seem to work. At the time I expect the notification, it doesn't show up.
I'm using a BroadcastReceiver and the AlarmManager to make a notification at the desired time. Here's my (simplified) code.
Code to set the time:
try {
Date date = format.parse(timeInput);//This part works
long time = date.getTime();//Get the time in milliseconds
Intent i = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent alarmSender = PendingIntent.getBroadcast(getBaseContext(), 0, i, 0);
AlarmManager am = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, time, alarmSender);
Toast.makeText(getBaseContext(), "Keep the app running to receive a reminder notification", Toast.LENGTH_LONG).show();
super.onBackPressed();
}catch(Exception e){
Toast.makeText(getBaseContext(), "Parsing error. Format:\ndd/MM/yyyy and HH:mm", Toast.LENGTH_SHORT).show();
}
The AlarmReceiver.onReceive() method:
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MenuActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0);
NotificationCompat.Builder nBulder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notify_icon)
.setContentTitle("title")
.setContentText("text")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat nManager = NotificationManagerCompat.from(context);
nManager.notify(0, nBulder.build());
}
Everything is properly declared in the manifest file.
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"></receiver>
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
throw new UnsupportedOperationException("Not yet implemented");
}
}
Minor Changes:
try {
long time = System.currentTimeMillis();
Intent i = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent alarmSender = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0);
AlarmManager am = (AlarmManager) getApplication().getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, time, alarmSender);
} catch(Exception e){
Toast.makeText(getBaseContext(), "Parsing error. Format:\ndd/MM/yyyy and HH:mm", Toast.LENGTH_SHORT).show();
}
Difference between getContext() , getApplicationContext() , getBaseContext() and "this"
I've found another way to do it. Instead of using a BroadcastListener and the AlarmManager, I'm using a new Thread. It waits until System.currentTimeMillis() == time and runs a runnable on the UI thread using runOnUIThread(). In that runnable, a notification is made.
I don't know if this is a good/efficient solution, but it does the job fine.

Android Java Notification To Alarm

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.

When user taps on the push notification message, my app launches. How to make the app display the push message in full?

Good day!
I have successfully sent push notification message to my app on android, and when I tap on the message, it launches my app. May I ask how do I pass intent/bundle to MainActivity from the push notification so that when the app is launched, it can display the push notification message in full within the app? Thank you very much!
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmMessageHandler will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmMessageHandler.class.getName());
showNotification(context, intent);
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
private void showNotification(Context context, Intent intent) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
String title = intent.getExtras().getString("nTitle");
String message = intent.getExtras().getString("nMessage");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context);
Notification notification = mBuilder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.face)
.setColor(context.getResources().getColor(R.color.wallet_holo_blue_light))
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.fuckya))
.setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message).build();
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
Regards,
Lorkh
Replace to:
Intent mainActivityIntent = new Intent(context, MainActivity.class);
String title = intent.getExtras().getString("nTitle");
String message = intent.getExtras().getString("nMessage");
mainActivityIntent.putExtra("nTitle", title);
mainActivityIntent.putExtra("nMessage",message);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, mainActivityIntent), 0);
And retrieve this values inside your MainActivity via getIntent();

Android - Vibrate without displaying new Activity?

I have the following code in an android app. What it currently does is, at the specified time passed with the Calendar when variable, it opens up the RunningActivity (which is blank), vibrates, and sends a notification. Even if I've pressed the home button and it's running in the background, it starts a new blank RunningActivity and vibrates and sends a notification. I'm trying to figure out how to do all the stuff in the RunningActivity (Vibrate and send a notification) without opening up the blank RunningActivity, allowing the application to stay in the background.
I do NOT need help with actually calling the notification or vibration. I just need to know how to run the actions in the RunningActivity onCreate at a specific time witout opening/showing the RunningActivity. As seen below, I setup a PendingIntent with an AlarmManagager, the issue is that it is launching an Activity and showing it when all I want it to do is vibrate/send a notification.
public void startAlarm(Activity activity, Calendar when){
currentTimerHour = when.get(Calendar.HOUR);
currentTimerMin = when.get(Calendar.MINUTE);
Intent intent = new Intent(activity, RunningActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(activity, 12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)activity.getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pendingIntent);
}
RunningActivity Class:
public class RunningActivity extends Activity {
#Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
NotificationCompat.Builder mBuilder;
mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.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(0, mBuilder.build());
}
}
To send Notification From Background:
public void createNotification(Context context) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(context,Myexample.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification noti = new Notification.Builder(context)
.setContentTitle("My Title")
.setContentText("My message.")
.setSmallIcon(R.drawable.app_icon)
.setContentIntent(pIntent).build();
#SuppressWarnings("static-access")
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
noti.flags |= Notification.FLAG_SHOW_LIGHTS;
notificationManager.notify(0, noti);
}
You can use service. Add your vibrator class on Service and call it when you need need it. You can also create a method on your class and call it when you need it.
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);

Categories