Можем ли мы сделать уведомление из WebApp? - PullRequest
1 голос
/ 02 ноября 2019

мы можем сделать уведомление из WebApp?

Я читаю в Интернете учебники о том, как сделать веб-приложение. Я связал свою HTML-страницу с Javascript-интерфейсом, чтобы сделать тост. Пока там это работает очень хорошо. Но когда я пытаюсь сделать уведомление, оно ничего не возвращает.

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

1 Ответ

0 голосов
/ 02 ноября 2019

NotificationCompat.Builder(Context) устарело. Вы должны использовать NotificationCompat.Builder(Context, CHANNEL_ID). И прежде чем использовать CHANNEL_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);
        }
    }
...