Alternative way on how to change setLatestEventInf

2020-03-30 04:27发布

问题:

Hi i got stock in this point wherein "setLatesEventInfo" has an error. i know that setLatestEventInfo doesnt run on API 23 and up. can someone help me on how to make this code run ? i mean alternative way , same function but different coded. this is a receiver function. i am doing a notification on my Mainactivity. The only error here is the receiver ,i tried the builder notification , but since this is a receiver ,builder cant apply

Receiver.java

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * Created by my pc on 12/1/2015.
 */
public class Receiver extends BroadcastReceiver {

    private static final Object ACTION = "MyNotification";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION.equals(action)) {
            //do what you want here
            Variables.numMessages++;
            generateNotification(context,"MyNotification");
        }
    }

    private void generateNotification(Context context, String message) {
        long when = System.currentTimeMillis();
        int icon = R.drawable.ic_od_icon_24dp;
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        String title = context.getString(R.string.app_name);
        // String subTitle = context.getString(R.string.app_name);
        String subTitle = "some text";
        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.putExtra("content", message);
        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        notification.setLatestEventInfo(context, title, subTitle, intent);
        //To play the default sound with your notification:
        //notification.defaults |= Notification.DEFAULT_SOUND;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);
    }

}

MainActivity.java

public class MainActivity extends Activity {

    int NOTIFICATION_ID = 1;
    int LED_ON_MS = 200;
    int LED_OFF_MS = 600;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onGenNoti(View v) {

        // Check readCount
        Variables.readCount = Variables.numMessages;

        PendingIntent pIntent =
                PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this, MainActivity.class), 0);

        if (Variables.numMessages > 0) {
            Notification noti = new Notification.Builder(MainActivity.this)
                    .setTicker("You Have " + Variables.numMessages + " message")
                    .setContentTitle("test")
                    .setContentText("")
                    .setSmallIcon(R.drawable.ic_od_icon_24dp)
                    .setContentIntent(pIntent).getNotification();
            noti.defaults |= Notification.DEFAULT_SOUND;
            noti.defaults |= Notification.DEFAULT_VIBRATE;
            noti.ledARGB = Color.BLUE;
            noti.ledOnMS = LED_ON_MS;
            noti.ledOffMS = LED_OFF_MS;
            noti.flags = Notification.FLAG_SHOW_LIGHTS;
            NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            nm.notify(NOTIFICATION_ID, noti);
        }
    }
}

回答1:

Now you have to generate your notification like this

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

    NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context);

    // set intent so it does not start a new activity
    Notification notification  = builder
            .setContentIntent(yourPendingIntent)
            .setSmallIcon(icon)
            .setWhen( System.currentTimeMillis();)
            .setContentTitle(title)
            .setContentText(message).build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(notifId, notification);