androidandroid-intentnotificationsandroid-phone-call

How do I make a notification that makes a call to a number?


I want it to create a pendingIntent notification after clicking on a button on the screen that says 'notify'. So when the user clicks on the notification it will dial a number like 021-1234567. How do I do this?

I have been searching online for help but I can't seem to find anything relating to this.

public void notifyPendingIntent(View view) {

    Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+ 0211234567));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

    PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

    // Build notification
    Notification notify = new Notification.Builder(this)
            .setContentTitle("Make this call")
            .setContentText("New call must be made to 021 123 4567")
            .setTicker("New Alert")
            .setContentIntent(pIntent).build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // hide the notification after its selected
    notify.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, notify);
}

I have this so far, but when I hit the button, nothing happens.

I tried this and it now works. Thanks to everyone who tried to help.

public void notifyPendingIntent(View view) {

    NotificationCompat.Builder myNotification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.my_notification_icon)
            .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());

}

Solution

  • <!-- NOTE! Your uses-permission must be outside the "application" tag
               but within the "manifest" tag. -->
    
    <uses-permission android:name="android.permission.CALL_PHONE" />
    

    Try this

    public void call() {
        Toast.makeText(this,"call",Toast.LENGTH_SHORT).show();
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel: 0211234567"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, callIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
    
    
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        notificationBuilder.setStyle(inboxStyle);
        inboxStyle.setBigContentTitle("sdjshfjnds");
        inboxStyle.addLine("sdjjsdfn");
        notificationBuilder.setStyle(inboxStyle);
    
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int NOTIFICATION_ID = 100;
        notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
    }