Last active
June 22, 2021 09:21
-
-
Save Dcosta2205/50651857db5ec6c986a15e091632bb0c to your computer and use it in GitHub Desktop.
Creating service to show headsup notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Notification; | |
import android.app.NotificationChannel; | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.media.AudioAttributes; | |
import android.media.AudioManager; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.os.IBinder; | |
import androidx.annotation.Nullable; | |
import androidx.core.app.NotificationCompat; | |
import java.util.Objects; | |
public class HeadsUpNotificationService extends Service { | |
private String CHANNEL_ID = "VoipChannel"; | |
private String CHANNEL_NAME = "Voip Channel"; | |
@Nullable | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
Bundle data = null; | |
if (intent != null && intent.getExtras() != null) { | |
data = intent.getBundleExtra(ConstantApp.FCM_DATA_KEY); | |
} | |
try { | |
Intent receiveCallAction = new Intent(getContext(), HeadsUpNotificationActionReceiver.class); | |
receiveCallAction.putExtra(ConstantApp.CALL_RESPONSE_ACTION_KEY, ConstantApp.CALL_RECEIVE_ACTION); | |
receiveCallAction.putExtra(ConstantApp.FCM_DATA_KEY, data); | |
receiveCallAction.setAction("RECEIVE_CALL"); | |
Intent cancelCallAction = new Intent(getContext(), HeadsUpNotificationActionReceiver.class); | |
cancelCallAction.putExtra(ConstantApp.CALL_RESPONSE_ACTION_KEY, ConstantApp.CALL_CANCEL_ACTION); | |
cancelCallAction.putExtra(ConstantApp.FCM_DATA_KEY, data); | |
cancelCallAction.setAction("CANCEL_CALL"); | |
PendingIntent receiveCallPendingIntent = PendingIntent.getBroadcast(getContext(), 1200, receiveCallAction, PendingIntent.FLAG_UPDATE_CURRENT); | |
PendingIntent cancelCallPendingIntent = PendingIntent.getBroadcast(getContext(), 1201, cancelCallAction, PendingIntent.FLAG_UPDATE_CURRENT); | |
createChannel(); | |
NotificationCompat.Builder notificationBuilder = null; | |
if (data != null) { | |
notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID) | |
.setContentText(data.getString("remoteUserName")) | |
.setContentTitle("Incoming Voice Call") | |
.setSmallIcon(R.drawable.ic_call_green) | |
.setPriority(NotificationCompat.PRIORITY_HIGH) | |
.setCategory(NotificationCompat.CATEGORY_CALL) | |
.addAction(R.drawable.ic_call_green, "Receive Call", receiveCallPendingIntent) | |
.addAction(R.drawable.ic_cancel_sexy, "Cancel call", cancelCallPendingIntent) | |
.setAutoCancel(true) | |
.setSound(Uri.parse("android.resource://" + getContext().getPackageName() + "/" + R.raw.voip_ringtone)) | |
.setFullScreenIntent(receiveCallPendingIntent, true); | |
} | |
Notification incomingCallNotification = null; | |
if (notificationBuilder != null) { | |
incomingCallNotification = notificationBuilder.build(); | |
} | |
startForeground(120, incomingCallNotification); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return START_STICKY; | |
} | |
/* | |
Create noticiation channel if OS version is greater than or eqaul to Oreo | |
*/ | |
public void createChannel() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT); | |
channel.setDescription("Call Notifications"); | |
channel.setSound(Uri.parse("android.resource://" + getContext().getPackageName() + "/" + R.raw.voip_ringtone), | |
new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) | |
.setLegacyStreamType(AudioManager.STREAM_RING) | |
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION).build()); | |
Objects.requireNonNull(getContext().getSystemService(NotificationManager.class)).createNotificationChannel(channel); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm passing some data (some params like doctor name, patient id ) in the form bundle from GCMService to HeadsUpNotificationService. So the Key name is FCM_DATA_KEY. If you don't need it then you can remove it