Notification.setLatestEventInfo symbol not found and cannot be compiled

Asked 2 years ago, Updated 2 years ago, 29 views

I am creating a program that displays notifications, but the notification.setLatestEventInfo part cannot be compiled because the symbol is not found.

import android.support.v7.app.AppCompatActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class screen2 extensions AppCompatActivity {

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

        String url="http://www.google.com/";
        Uri = Uri.parse(url);

        Intent=new Intent(Intent.ACTION_VIEW,uri);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);

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

        Notification notification=new Notification();

        notification.flags=Notification.FLAG_AUTO_CANCEL;


        notification.tickerText="text";

        // Cannot compile due to missing symbol for this part
        notification.setLatestEventInfo(
                getApplicationContext(),
                "Title",
                "Message",
                pendingIntent
        );


        nManager.notify(1,notification);
    }
}

android java

2022-09-30 14:22

1 Answers

For Notification, Japanese document exists, so please refer to it first.

Notification styles have been added and changed for each version, and the API has also changed significantly.setLatestEventInfo() is a very legacy method that has long been obsolete, but has been removed at API level 23 (Android Marshmallow).

NotificationCompat is provided in the support library to absorb API differences between these versions.Except in rare cases where only the latest SDK version is supported, you should basically try this one.

Intent=newIntent(Intent.ACTION_VIEW, Uri.fromParts("http", "www.google.com", null));
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);

NotificationManagerCompatmanager=NotificationManagerCompat.from(this);
Notification notification=new NotificationCompat.Builder(this)
        .setAutoCancel(true)
        .setContentTitle("title")
        .setContentText("content text")
        .setTicker("ticker text")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notify_icon))
        .setContentIntent(pendingIntent)
        .build();

manager.notify(0,notification);


2022-09-30 14:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.