gpt4 book ai didi

android - 我们可以从 WebApp 发出通知吗?

转载 作者:行者123 更新时间:2023-11-30 04:58:38 24 4
gpt4 key购买 nike

我们可以从 WebApp 发出通知吗?

我正在互联网上阅读有关如何制作网络应用程序的教程。我用 Javascript 接口(interface)绑定(bind)了我的 html 页面来制作 Toast。直到那里它工作得很好。但是当我尝试发出通知时,它什么也没返回。

主要 Activity .java

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;

public class MainActivity extends Activity {

private WebView mWebView;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

mWebView.loadUrl("url of my webapp");
mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");

}
public class WebAppInterface {
Context mContext;

/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}

/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Intent intent = new Intent();//**The activity that you want to open when the notification is clicked
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(android.R.drawable.star_on)
.setContentTitle("FCM Message")
.setAutoCancel(true)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
}

最佳答案

NotificationCompat.Builder(Context) 已弃用。您必须使用 NotificationCompat.Builder(Context, CHANNEL_ID)。在使用 CHANNEL_ID 之前,您必须先像这样创建一个 channel 。

private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_desc);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(Utility.CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);

if(notificationManager!=null)
notificationManager.createNotificationChannel(channel);
}
}

关于android - 我们可以从 WebApp 发出通知吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58668285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com