消息点击回调类
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MessageActivity extends AppCompatActivity {
private Context context;
public MessageActivity(Context context) {
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 消息点击回调事件
Toast.makeText(context, "点击消息", Toast.LENGTH_LONG).show();
}
}
JS点击推送消息
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.webkit.JavascriptInterface;
import android.widget.Toast;
import java.util.Random;
public class AndroidJs extends AppCompatActivity {
private Context mContext;
public AndroidJs(Context context) {
this.mContext = context;
}
@JavascriptInterface
public void notifyme() {
String title = "这是标题";
String message = "这是内容";
int nid = new Random().nextInt();
Notification notification = null;
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
// 结果处理
Intent intent = new Intent(mContext, MessageActivity.class);
intent.putExtra("data", message);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(mContext, nid, intent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "demo";
String channelName = "示例";
NotificationChannel mChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
mChannel.enableLights(true);
mChannel.enableVibration(true);
notificationManager.createNotificationChannel(mChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext)
// 区别
.setChannelId(channelId)
.setContentIntent(pi)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher);
notification = notificationBuilder.build();
} else {
Notification.Builder notificationBuilder = new Notification.Builder(mContext)
.setContentIntent(pi)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
// 区别
.setOngoing(true);
notification = notificationBuilder.build();
}
notificationManager.notify(nid, notification);
}
主类
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
// 获取WebSetting对象
WebSettings webSettings = webView.getSettings();
// 设置支持javascript
webSettings.setJavaScriptEnabled(true);
// 将Android里面定义的类对象AndroidJs暴露给javascript
webView.addJavascriptInterface(new AndroidJs(MainActivity.this), "AndroidJs");
//覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://10.13.144.172/android");
}
}
网友评论