Android 我们可以通过网络应用程序发出通知吗?

Android 我们可以通过网络应用程序发出通知吗?,android,android-intent,android-webview,Android,Android Intent,Android Webview,我们可以通过网络应用程序发出通知吗 我正在网上阅读有关如何制作网络应用程序的教程。我用Javascript界面绑定了我的html页面来敬酒。在那之前,它工作得很好。但是当我尝试发出通知时,它什么也不返回 MainActivity.java import android.annotation.SuppressLint; import android.app.Activity; import android.app.NotificationManager; import android.app.Pe

我们可以通过网络应用程序发出通知吗

我正在网上阅读有关如何制作网络应用程序的教程。我用Javascript界面绑定了我的html页面来敬酒。在那之前,它工作得很好。但是当我尝试发出通知时,它什么也不返回

MainActivity.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(上下文)
不推荐使用。您必须使用
NotificationCompat.Builder(上下文,频道ID)
。在使用
CHANNEL\u ID
之前,您必须先创建一个这样的频道

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);
        }
    }

NotificationCompat.Builder(上下文)
不推荐使用。您必须使用
NotificationCompat.Builder(上下文,频道ID)
。在使用
CHANNEL\u ID
之前,您必须先创建一个这样的频道

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);
        }
    }