1

我正在遵循本指南,但我对其进行了一些修改;
删除了包含所有电子邮件和名称内容的第一个活动。基本上,我所做的是:
一个带有按钮和文本视图的应用程序,当您按下按钮时,regId 会弹出。到目前为止一切都很好,但是当谈到接收推送本身时,没有弹出窗口,没有唤醒锁或任何东西,只是“通知中心”中的一个简单行(真的不知道它在 android 上叫什么)。继承人的代码:

private static void generateNotification(Context context, String message) 
    {
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = context.getString(R.string.app_name);

        Intent notificationIntent = new Intent(context, MainActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

错误:
通知通知 = new Notification(icon, message, when);

构造函数 Notification(int, charSequence, long) 已弃用

notification.setLatestEventInfo(context, title, message, intent);
The method setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent) from the type Notification is deprecated

(logCat 从错误中清除)

当我打开声明时,它显示“JAR 文件没有源附件”。
我试图添加来源和谷歌搜索。但无论我做什么它都说

the source attachment does not contain the source for the file Notification.class

我相信我在推送中获取消息的问题是因为这个。关于如何解决它的任何想法?

PS。我是这一切的新手,如果您需要更多代码,请告诉我,如果我在这里走错了路,请告诉我!:)

4

1 回答 1

2

这与您的警告无关。警告只是说,您使用的方法自 API 级别 11 起已弃用。对于较新的 API,您可以(但您不必这样做,只是建议)使用 Notification.Builder:

Notification noti = new Notification.Builder(mContext)
     .setContentTitle("New mail from " + sender.toString())
     .setContentText(subject)
     .setSmallIcon(R.drawable.new_mail)
     .setLargeIcon(aBitmap)
     .build();

编辑:检查当前 API:

int currentVersion = android.os.Build.VERSION.SDK_INT;
int honeycombVersion = android.os.Build.VERSION_CODES.HONEYCOMB;

if (currentVersion >= honeycombVersion ){
    // Use Notification.Builder
} else{
    // Use Notification(int, charSequence, long)
}

Edit2:如果您使用支持库,您可以在较低的 API 上使用 Notification.Builder。

于 2012-12-16T18:34:19.487 回答