【问题标题】:Status bar notification plugin error with CordovaCordova 的状态栏通知插件错误
【发布时间】:2012-07-20 14:37:14
【问题描述】:

我正在尝试将 Cordova 的状态栏通知插件添加到我的 Android 应用程序中,但它的代码出现错误。

这是有问题的代码:

  Notification noti = new Notification.Builder(context)
    .setContentTitle(contentTitle)
    .setContentText(contentText)
    .setSmallIcon(icon)
    .build();

错误在.build(),Eclipse 告诉我:

“Notification.Builder 类型的方法 build() 未定义”

【问题讨论】:

    标签: android eclipse cordova phonegap-plugins


    【解决方案1】:

    我也有同样的问题。看起来与 sdk 版本不匹配,现在已弃用的方法。

    getNotification() 是从 API 11 开始调用的方法 在 API 16 中添加了 build()

    如果你和我一样,你使用的是

    我现在不会担心 API 16,但我敢打赌,如果我下载 16 并将我的目标设置为这样,build() 将起作用。

    让我知道它是否适合你。

    【讨论】:

    • 谢谢!是的,我的目标是 API 15。现在它运行良好。
    • 即使在使用 .getNotification() 之后,.java 文件中的错误对我来说已经消失了,但插件无法正常工作。
    • @CodeGuru 您还必须修改javascript文件,我认为它是为旧版本的cordova制作的。您可以使用以下代码: window.notify = function(title, body) { cordova.exec(null, null, 'StatusBarNotification', 'notify', [title, body]); }; window.clear = function() { cordova.exec(null, null, 'StatusBarNotification', 'clear', []);然后只需从您的代码中调用 notify 或 clear 函数。
    【解决方案2】:

    对我来说 .getNotification() 没有解决问题,因为我需要 API 10 及更高版本的解决方案。

    我找到了处理它的方法。如果其他人有同样的问题,我建议这样做:

    1) 查看 StatusBarNotification (click) 的说明

    2) 修改StatusBarNotification.java

    • 添加

      私人通知通知;

      private PendingIntent contentIntent;

    在 StatusBarNotification 类的底部,例如在 NotificationManager 声明之前
    • 修改 showNotification 方法

    评论或删除:

    导入android.app.Notification.Builder;

    Notification noti = new Notification.Builder(context) .setContentTitle(contentTitle) .setContentText(contentText) .setSmallIcon(icon) .build();

    粘贴而不是这部分:

    noti = new Notification(android.R.drawable.btn_star_big_on, contentText, System.currentTimeMillis() );
    noti.flags = Notification.FLAG_AUTO_CANCEL;
    
    
    Intent notificationIntent = new Intent(context, !yourMainActivityClass!.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    
    noti.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    • 更改 !yourMainActivityClass!到你的班级
    • 在 index.html 中添加调用方法对于测试,您可以使用 JQM 按钮

      onclick='window.plugins.statusBarNotification.notify("把你的标题 这里", "把你的信息放在这里");return false;'

    我知道这个解决方案使用了折旧的方法,但我花了很多时间让它工作,我没有看到 API 10 的另一个解决方案。如果有人有更好的想法,请与我分享 ;)

    【讨论】:

      猜你喜欢
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      相关资源
      最近更新 更多