附上缺少的源码android

时间:2012-12-16 18:25:25

标签: android push-notification push

我正在关注this guide,但我重新制作了一下;
删除了第一个包含所有电子邮件和名称内容的活动。基本上,我所做的是:
带有按钮和文本视图的应用程序,当您按下按钮时,会弹出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;

错误:
通知通知=新通知(图标,消息,何时);

  

不推荐使用构造函数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文件没有源附件” 我试图添加源和googleing。但无论我做什么都说

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

我认为我在推送中获取消息的问题是因为这个原因。关于如何修复它的任何想法?

PS。我对这一切都不熟悉,如果您需要更多代码,请告诉我并告诉我这里是否有错误的轨道!:)

1 个答案:

答案 0 :(得分: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。

相关问题