Skip to content

Instantly share code, notes, and snippets.

@DrBrad
Last active September 8, 2022 02:06
Show Gist options
  • Save DrBrad/7574712c6140ccc468212afa04d9e458 to your computer and use it in GitHub Desktop.
Save DrBrad/7574712c6140ccc468212afa04d9e458 to your computer and use it in GitHub Desktop.
Android Media WebView Notifications
//-------- add this to webview ----------
wv.setWebChromeClient(new webChromeClient());
wv.addJavascriptInterface(new JSInterface(), "JSOUT");
//----- add this to manifest under </activity> -----
<receiver android:name=".NotificationPausePlay" />
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="#e6e6e6">
<ImageView
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/icon"
android:src="@drawable/icon"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginRight="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:textSize="16dp"
android:textStyle="bold"
android:textColor="#000000"
android:singleLine="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/icon"
android:layout_toEndOf="@+id/icon"
android:layout_toLeftOf="@+id/pausePlay"
android:layout_toStartOf="@+id/pausePlay" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/desc"
android:textSize="16dp"
android:textColor="#5f5f5f"
android:singleLine="true"
android:layout_below="@+id/title"
android:layout_toRightOf="@+id/icon"
android:layout_toEndOf="@+id/icon"
android:layout_toLeftOf="@+id/pausePlay"
android:layout_toStartOf="@+id/pausePlay"/>
<ImageButton
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/pausePlay"
android:scaleType="fitXY"
android:background="#e6e6e6"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
public class JSInterface {
public static boolean playing;
@JavascriptInterface
public void mediaAction(String type){
Log.e("Info", type);
playing = Boolean.parseBoolean(type);
if(playing){
NotificationManager mNotificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(activity);
Notification notification = builder.getNotification();
notification.icon = R.drawable.icon;
RemoteViews contentView = new RemoteViews(activity.getPackageName(), R.layout.custom_notification);
notification.contentView = contentView;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
contentView.setTextViewText(R.id.title, "Browser");
contentView.setTextViewText(R.id.desc, "This is a description. - PLAYING");
contentView.setImageViewResource(R.id.pausePlay, R.drawable.pause);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(activity, 0, new Intent(activity, NotificationPausePlay.class), 0);
contentView.setOnClickPendingIntent(R.id.pausePlay, pendingSwitchIntent);
mNotificationManager.notify(99, notification);
}else{
NotificationManager mNotificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(activity);
Notification notification = builder.getNotification();
notification.icon = R.drawable.icon;
RemoteViews contentView = new RemoteViews(activity.getPackageName(), R.layout.custom_notification);
notification.contentView = contentView;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
contentView.setTextViewText(R.id.title, "Browser");
contentView.setTextViewText(R.id.desc, "This is a description. - PAUSED");
contentView.setImageViewResource(R.id.pausePlay, R.drawable.play);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(activity, 0, new Intent(activity, NotificationPausePlay.class), 0);
contentView.setOnClickPendingIntent(R.id.pausePlay, pendingSwitchIntent);
mNotificationManager.notify(99, notification);
}
}
}
public class NotificationPausePlay extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
Log.e("Here", "I am here");
if(playing){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
wv.evaluateJavascript("mediaElement.pause();", null);
}else{
wv.loadUrl("javascript:mediaElement.pause();");
}
NotificationManager mNotificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(activity);
Notification notification = builder.getNotification();
notification.icon = R.drawable.icon;
RemoteViews contentView = new RemoteViews(activity.getPackageName(), R.layout.custom_notification);
notification.contentView = contentView;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
contentView.setTextViewText(R.id.title, "Browser");
contentView.setTextViewText(R.id.desc, "This is a description. - PAUSED");
contentView.setImageViewResource(R.id.pausePlay, R.drawable.play);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(activity, 0, new Intent(activity, NotificationPausePlay.class), 0);
contentView.setOnClickPendingIntent(R.id.pausePlay, pendingSwitchIntent);
mNotificationManager.notify(99, notification);
playing = false;
}else{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
wv.evaluateJavascript("mediaElement.play();", null);
}else{
wv.loadUrl("javascript:mediaElement.play();");
}
NotificationManager mNotificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(activity);
Notification notification = builder.getNotification();
notification.icon = R.drawable.icon;
RemoteViews contentView = new RemoteViews(activity.getPackageName(), R.layout.custom_notification);
notification.contentView = contentView;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
contentView.setTextViewText(R.id.title, "Browser");
contentView.setTextViewText(R.id.desc, "This is a description. - PLAYING");
contentView.setImageViewResource(R.id.pausePlay, R.drawable.play);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(activity, 0, new Intent(activity, NotificationPausePlay.class), 0);
contentView.setOnClickPendingIntent(R.id.pausePlay, pendingSwitchIntent);
mNotificationManager.notify(99, notification);
playing = true;
}
}
}
@Override
public void onProgressChanged(final WebView view, int newProgress){
super.onProgressChanged(view, newProgress);
if(newProgress == 100){
String code = "var mediaElement;" +
"mediaCheck();" +
"document.onclick = function(){" +
" mediaCheck();" +
"};" +
"function mediaCheck(){" +
" for(var i = 0; i < document.getElementsByTagName('video').length; i++){" +
" var media = document.getElementsByTagName('video')[i];" +
" media.onplay = function(){" +
" mediaElement = media;" +
" JSOUT.mediaAction('true');" +
" };" +
" media.onpause = function(){" +
" mediaElement = media;" +
" JSOUT.mediaAction('false');" +
" };" +
" }" +
" for(var i = 0; i < document.getElementsByTagName('audio').length; i++){" +
" var media = document.getElementsByTagName('audio')[i];" +
" media.onplay = function(){" +
" mediaElement = media;" +
" JSOUT.mediaAction('true');" +
" };" +
" media.onpause = function(){" +
" mediaElement = media;" +
" JSOUT.mediaAction('false');" +
" };" +
" }" +
"}";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
wv.evaluateJavascript(code, null);
}else{
wv.loadUrl("javascript:"+code);
}
}
}
@gfyre
Copy link

gfyre commented Apr 21, 2019

Hi. I implemented all of your codes in the exact manner as you had suggested. However, for starters, it gave me too many errors about "wv" not existing and something about the "play" "pause" xmls. After sloppily working around those errors, I ran the app but the controls didn't come up in the Notification Center. what did I miss?

@marcqtan
Copy link

Hi, how to properly pass "webview" into NotificationPausePlay.java ?

@rohangho
Copy link

Hi. I implemented all of your codes in the exact manner as you had suggested. However, for starters, it gave me too many errors about "wv" not existing and something about the "play" "pause" xmls. After sloppily working around those errors, I ran the app but the controls didn't come up in the Notification Center. what did I miss?

Actually, this code is old after android O you need to create a notification channel to pass notification

Check my implementation
https://github.com/rohangho/DemoDownloadPage/tree/Mp3Broadcast

Thanks
@DrBrad

@DrBrad
Copy link
Author

DrBrad commented Oct 29, 2020

Thank you, I haven't maintained it in over a year.

@shaktiraj9222
Copy link

Hi. I implemented all of your codes in the exact manner as you had suggested. However, for starters, it gave me too many errors about "wv" not existing and something about the "play" "pause" xmls. After sloppily working around those errors, I ran the app but the controls didn't come up in the Notification Center. what did I miss?

Actually, this code is old after android O you need to create a notification channel to pass notification

Check my implementation
https://github.com/rohangho/DemoDownloadPage/tree/Mp3Broadcast

Thanks
@DrBrad

any solution in java??

@DrBrad
Copy link
Author

DrBrad commented Sep 2, 2021

Okay, "wv" is your webview. Play / Pause xmls are your play / pause images, it can be whatever you want. As far as android O I will probably need to rewrite this because the notification system has been deprecated. If you want to fix this, I recommend looking at the new method for building a custom notification then you can change that portion of the code that I wrote.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment