Какой метод я должен использовать вместо setLatestInfo? - PullRequest
0 голосов
/ 29 марта 2020

Я новичок и пытаюсь показать пользователю уведомление в строке состояния. Я обнаружил, что API23 + не поддерживает метод setLatestInfo, поэтому мой компилятор находит его как ошибку. Какой другой метод я должен использовать вместо этого, чтобы включить уведомления?

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import androidx.annotation.RequiresApi;

import static android.app.PendingIntent.getActivity;
import static android.content.Context.NOTIFICATION_SERVICE;


public class Settings extends AppCompatActivity {
    Switch simpleswitch1;
    Switch simpleswitch2;
    private Notification notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);


        simpleswitch1 = (Switch) findViewById(R.id.simpleswitch1);
        simpleswitch2 = (Switch) findViewById(R.id.simpleswitch2);
        simpleswitch1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Notify("Title", "Message");
            }


        });
        simpleswitch2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
                    notification.defaults |= Notification.DEFAULT_SOUND;




                }});}


    private void Notify(String notificationTitle, String notificationMessage) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.notification, "New message", System.currentTimeMillis());

        Intent notificationIntent = new Intent(Settings.this, Settings.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        notification.setLatestEventInfo(Settings.this, notificationTitle, notificationMessage, pendingIntent);
        notificationManager.notify(9999, notification);
    }




    }

1 Ответ

0 голосов
/ 29 марта 2020

setLatestEventInfo равно устарело , удалено в API 23. Поэтому, если ваша версия SDK установлена ​​на API 23+, у вас возникнет эта проблема, и вам следует использовать NotificationCompat.Builder вместо.

Пример:

notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

private void Notify(String notificationTitle, String notificationMessage) {
    Intent notificationIntent = new Intent(Settings.this, Settings.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification)
        .setContentTitle("New message")
        .setContentText("My notification")
        // Set the intent that will fire when the user taps the notification
        .setContentIntent(pendingIntent)
        .build();

    Notification notification = builder.getNotification();
    notificationManager.notify(11 /*An identifier for this notification unique within your application.*/, notification);

}

Предлагаю ознакомиться с официальной документацией об уведомлении здесь .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...