【问题标题】:Android show notification from BroadcastReceiver来自 BroadcastReceiver 的 Android 显示通知
【发布时间】:2011-10-23 20:13:21
【问题描述】:

我有一个扩展 BroadcastReceiver 的类,只要有新的 Wifi 扫描结果可用(接收器在清单中注册,Scan_Results 广播作为意图过滤器)就会被调用。

通过这个类,我希望能够向用户显示通知。目前,我将在我的广播意图类的 onReceive 方法中作为参数接收的上下文传递给另一个类的“显示通知”方法。

当它到达线路时:

myNotificationManager.notify(notificationId, notification);

它失败并出现以下异常:

java.lang.IllegalArgumentException: contentView required: pkg=com.mumfordmedia.trackify id=2131034122 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0)

知道为什么会这样吗?我能想到的只是因为我从 onReceive 参数中获得的上下文不是......因为没有更好的短语“适合工作”......

有什么想法吗? 谢谢, 最大。

【问题讨论】:

  • 也许stackoverflow.com/questions/2826786/… 会有所帮助。无论如何,请向我们展示更多代码,以便我们提供帮助。
  • 请向我们展示更多您的代码,以便我们更好地了解正在发生的事情。
  • 顺便说一下,欢迎来到 Stackoverflow!如果您的回复有帮助,请投票。如果回复成功回答了您的问题,请点击旁边的绿色复选标记接受答案。

标签: android notifications wifi broadcastreceiver


【解决方案1】:

ContentView 是点击通知时需要的视图。下面的代码工作正常,setLatestEventInfo() 是必需的方法。

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher,
            "Hello from service", System.currentTimeMillis());
    Intent intent = new Intent(this, MainActivity.class);
    notification.setLatestEventInfo(this, "contentTitle", "contentText",
            PendingIntent.getActivity(this, 1, intent, 0));
    manager.notify(111, notification);

【讨论】:

  • +1:使用 setLatestEventInfo() 会导致错误消失。显示这么简单的通知需要多少行代码,有点烦人……
【解决方案2】:

不确定为什么它以前不能正常工作,但这是我使用的代码:

在任何方法之外声明以下内容:

int YOURAPP_NOTIFICATION_ID = 1234567890;
NotificationManager mNotificationManager;

然后在onReceive方法中调用如下:

mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
showNotification(context, R.drawable.icon, "short", false);

然后声明如下方法:

private void showNotification(Context context, int statusBarIconID, String string, boolean showIconOnly) {
        // This is who should be launched if the user selects our notification.
        Intent contentIntent = new Intent();

        // choose the ticker text
        String tickerText = "ticket text";

        Notification n = new Notification(R.drawable.icon, "ticker text", System.currentTimeMillis());

        PendingIntent appIntent = PendingIntent.getActivity(context, 0, contentIntent, 0);

        n.setLatestEventInfo(context, "1", "2", appIntent);

        mNotificationManager.notify(YOURAPP_NOTIFICATION_ID, n);
    }

【讨论】:

    【解决方案3】:

    你必须调用 Notification.setLatestEventInfo()。

    【讨论】:

      【解决方案4】:

      将此代码与您的通知一起使用

      Intent intent = new Intent(this, MusicDroid.class);
      PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
      notification.setLatestEventInfo(this, "This is the title", 
          "This is the text", activity);
      notification.number += 1;
      
      nm.notify(NOTIFY_ID, notification);
      

      【讨论】:

        【解决方案5】:

        据我所知,当您创建通知以传递给通知管理器时,您并没有给它一个内容视图来显示。查看您实际创建通知的行,以查看您是否确实为通知提供了要显示的视图。

        【讨论】:

        • 我从应用程序的其他地方复制并粘贴了通知脚本。没有可用的内容视图,因为它是从一个扩展了没有布局的 BroadcastReceiver 的类激活的。为什么需要内容视图?我会在几个小时内查看文档...感谢您的回复顺便说一句
        【解决方案6】:

        对于那些正在使用 NotificationCompat 的人,以下代码将起作用:

        NotificationCompat.Builder n = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.icon).setContentText("Notify Title").setContentText("Sample Text");
            NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            Intent i = new Intent(this,MainActivity.class);
            PendingIntent ac = PendingIntent.getActivity(this, 0, i, 0);
            n.setContentIntent(ac);
            nm.notify(12222, n.build());
        

        【讨论】:

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