java - 关于如何更改通知接收器中的 setLatestEventInfo 的替代方法

标签 java android-activity notifications

嗨,我知道这一点,其中“setLatesEventInfo”有错误。我知道 setLatestEventInfo 不能在 API 23 及更高版本上运行。有人可以帮助我如何让这段代码运行吗?我的意思是替代方法,功能相同但编码不同。这是一个接收器函数。我正在对我的主要 Activity 进行通知。这里唯一的错误是接收器,我尝试了构建器通知,但由于这是接收器,构建器无法应用

接收器.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);
        }
    }
}

最佳答案

现在您必须像这样生成通知

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

关于java - 关于如何更改通知接收器中的 setLatestEventInfo 的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34012544/

相关文章:

java - 使用 ArrayList 显示另一个类的值

java - 应该在哪一层进行验证?

android - Parcelable 用于 inproc 通信的效率

android - getActivity().findViewById 返回 null,从 fragment onActivityCreated 调用

node.js - 如何在 AWS 上使用 Sockets 发送通知?

iphone - 如何知道 iPhone 中的 MPMoviePlayerController 何时已暂停?

java - JAVA 中的长轮询 Jquery?

java - 如何检测文件中的新行(或空行)?

android - 每次屏幕打开时重新创建应用程序类

android - 在通知中使用 RatingBar