Notification with pending intent won't make phone call - java

I have a notification with a pending intent and once clicked it is supposed to dial a number. The notification comes up but once clicked nothing happens. I have set permission for the app to make calls in the android manifest file as follows:
<uses-permission android:name="android.permission.CALL_PHONE" />
Below is the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating);
addListenerOnRatingBar();
NotificationCompat.Builder myNotification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("Calling 021-12345678")
.setContentTitle("Phone Call Notification");
Intent phoneCall = new Intent(Intent.ACTION_CALL);
phoneCall.setData(Uri.parse("tel:021-12345678"));
PendingIntent phoneCallIntent = PendingIntent.getActivity(this, 0, phoneCall, PendingIntent.FLAG_UPDATE_CURRENT);
myNotification.setContentIntent(phoneCallIntent);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, myNotification.build());
}
Any ideas?

Well, this is actually workd (LOL).
But since you are using a pending intent, it needs a triger to launch (Click on the notification and it will call ;-).
Try to replace to a simple intent.
Like this:
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number"));
startActivity(intent);
And it will work like a charm.
P.S. I'm don't really understand why are using a notification, since when you call someone the call is taking control over the screen.

Related

Running a function after the app opens from the background after notification click

I'm pretty new to Android development. I have been able to get a notification to pop up while the app is in the background. When I click on it, it successfully loads the application backup. However I want to load an Alert from the page but only when it is opened from a notification click.
Here is the code for generating the notification. Any help would be appreciated.
private void getNotificationForPasswordChange() {
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Hello";// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
if (mNotificationManager != null)
mNotificationManager.createNotificationChannel(mChannel);
}
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher);
Intent i=new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent mainIntent = PendingIntent.getActivity(this, 0,
i, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Pronto Tracker")
.setTicker("Pronto Tracker")
.setContentText("Cannot connect to server. Location is not being updated.")
.setSmallIcon(R.mipmap.ic_pronto_logo)
.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
.setOngoing(true).setContentIntent(mainIntent).
build();
mNotificationManager.notify(Constants.PASSWORD_CHANGE_NOTIFICATION_ID, notification);
}
You can pass the alert message with notification PendingIntent. Add the message or value you want to show as alert in PendingIntent .putExtra() and also specify the activity in PendingIntent where you want to show the alert in form of dialog or anything.
Intent intent = new Intent(Application.getAppContext(), MainActivity.class);
intent.putExtra("is_notification", true);
intent.putExtra("alert_message", "Hello World!");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent lowIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
After that add the PendingIntent to your notification.
Second thing you need to do is to get the data from the Intent when user taps on notification.
In your MainActivity add the following code to get data from Intent:-
if (getIntent() != null) {
String message = getIntent().getStringExtra("alert_message");
boolean isNotification = getIntent().getBooleanExtra("is_notification", false);
if(is_notification){
// show alert
}
}
You should use onCreate function on your MainActivity
Add this code to parce your intent:
Intent receivedIntent = getIntent();

Creating notification when app is closed

I am using the onDestroy method, which I have learned is not always called when app is closed. And this has created a problem for me, where I would like to have the notification pop up every time the app closed. Is there a better way to do this? By the way, onStop and onPause are not options for me, because my app runs as a background service. Interestingly, my onDestroy method works everytime my background service is running, but whenever it is turned off, and just the app interface is open, my onDestroy never is called. This seems weird because I thought that onDestroy isn't called if the device is lacking resources, and running my service would be taking up more resources than not. Idk. Any help on this would be greatly appreciated!
`#Override
public void onDestroy(){
super.onDestroy();
Log.d("asdasd","asdasdasdasdasd");
if(notif.isChecked()) {
Intent intent1 = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle("S-Dimmer")
.setContentText(getResources().getString(R.string.sDimmerStopped))
.setSmallIcon(R.drawable.ic_action_name1)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_NO_CLEAR;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, noti);
doas.isRunning = false;
}
}`
This method is not always called, so you should try from background service, as there's also some destruction process alert.
So please try to do what ever you want there in the services.
Please check out this ..
onDestroy() may or may not be called on any given activity or service. The general rule is that either onDestroy() is called, or your process is terminated, or your code crashed.
You don't. Your process may be terminated for any reason, at any time, by the user or by the OS. You may or may not be called with onDestroy() when that occurs. While you may be able to improve your success rate a little via onTaskRemoved() on a Service, this itself has proven unreliable, and it will not cover all scenarios.
you are doing it wrong way. In onDestroy method all other stuffs should implemented before super.onDestory()
try this
#Override
public void onDestroy(){
Log.d("asdasd","asdasdasdasdasd");
if(notif.isChecked()) {
Intent intent1 = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle("S-Dimmer")
.setContentText(getResources().getString(R.string.sDimmerStopped))
.setSmallIcon(R.drawable.ic_action_name1)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_NO_CLEAR;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, noti);
doas.isRunning = false;
}
super.onDestroy();
}`
onTaskRemoved()
{
if(notif.isChecked()) {
Intent intent1 = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0);
Notification noti = new Notification.Builder(this)
.setContentTitle("S-Dimmer")
.setContentText(getResources().getString(R.string.sDimmerStopped))
.setSmallIcon(R.drawable.ic_action_name1)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_NO_CLEAR;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, noti);
doas.isRunning = false;
}
}

System notification from SyncAdapter:onPerformSync

I have sync adapter that performs some operation in background. To notify my main activity about sync operation status, I used broadcast receivers, so my activity is able to receive messages from sync adapter. It works fine. However, I also need to display notification on android status bar, that indicates some sync results.
So I wrote simple method reponsible to disaply system notification:
private void sendNotification(Context ctx, String message)
{
Intent intent = new Intent(ctx, this.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Mobile Shopping")
.setContentText(message)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
.setLights(0xff00ff00, 300, 100)
.setPriority(Notification.PRIORITY_DEFAULT);
//.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 , notificationBuilder.build());
}
Then, above method is called in onPerform sync:
#Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult)
{
.................
sendNotification(context, message);
}
Context is retrieved from constructor. It works without any problems, notification is showing.
However I also need to show main activity after user cicks on notification. So I believe I need to create PendingIntent and pass it to my notification builder (as it's commented in my code). But to pass main activity object to my sync adapter? Notification can be also displayed after auto sync finished.
Any tips?
So, I figured it out. Solution is to create pending intent this way:
Intent notificationIntent = new Intent(ctx, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
So after user clicks to notification, main activity will be shown.

Notifications not shown in Android Froyo

I'm trying to run the following, which is ripped from an internet tutorial:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Prepare intent which is triggered if the
// notification is selected
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
//Intent intent = new Intent(this, ReceiveAndGoHome.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, homeIntent, 0);
// Build notification
// Actions are just fake
Notification noti = new NotificationCompat.Builder(this)
.setContentTitle("Fraz Go Home!!!")
.setContentTitle("Fraz Go Home!!!")
.setContentText("Fraz Go Home!!!")
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more", pIntent).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(25, noti);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
When I run the above in a froyo AVD, I do not see a notification appear at all - am I missing something obvious?
You forgot to set the small icon. Add this:
.setSmallIcon (R.drawable.ic_launcher)
somewhere in your NotificationCompat.Builder chain of methods.
Of course, the launcher drawable is what I used there since it's on hand, you will need to make actual notification icons for a proper UI. This is just to demo the method call you need.
The Android documentation outlines what you need for a Notification to show:
Required notification contents
A Notification object must contain
the following:
• A small icon, set by setSmallIcon()
• A title, set by setContentTitle()
• Detail text, set by setContentText()

How to clear notification on notification action click?

I'm developing an android app where I display notifications with actions. But on action click notification not clearing, It stuck in that shade. How do I clear a notification on action click?
MY CODE
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
Intent intent = new Intent(this, SettingsActivity.class);
PendingIntent openSettingsActivity = PendingIntent.getActivity(this,1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationBuilder.addAction(R.drawable.ic_notification_button, "Settings", openSettingsActivity);
notificationBuilder.setPriority(Notification.PRIORITY_MAX);
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(text);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setColor(color);
notificationBuilder.setSmallIcon(R.drawable.ic_notification);
notificationBuilder.setContentIntent(openSettingsActivity);
final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notificationBuilder.build());
Hiding notifications should be processed in the place where the intent is sent.
In your current code:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
Intent intent = new Intent(this, SettingsActivity.class);
intent.putExtra("hide_notification", true); //add boolean to check later in activity if it should remove notification on activity create
And in your activity smth like this, to check if it should remove notification:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//check for notification remove
boolean hideNotification = getIntent().getBooleanExtra("hide_notification", false);
if (hideNotification) {
NotificationManagerCompat nmc = NotificationManagerCompat.from(this);
nmc.cancel(1); //1 - is your notification id
}
}
Depends on what you want, maybe it will be better to call that not in onCreate() but onStart()

Categories

Resources