通知管理器不会显示任何类型的通知

时间:2013-03-11 12:19:29

标签: android notifications

我对Android很新,我有一个GCM客户端和服务器,它工作正常。当应用程序运行时,我从服务器获取通知。

但是当应用程序未运行时,我没有得到任何回复。

以下是我用于生成通知的代码:

   private void generateNotification(Context context, String message) {
        int icon = 1;
        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, DemoActivity.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;
        notificationManager.notify(0, notification);
    }

通过以下方式调用:

@Override
    protected void onMessage(Context context, Intent intent) {
        generateNotification(context, "Tock Tock");
    }

当我看到logcat时,我收到如下错误:

 Unable to find resource: 1
W/ImageView(   58): android.content.res.Resources$NotFoundException: Resource ID #0x1
W/ImageView(   58):     at android.content.res.Resources.getValue(Resources.java:892)
W/ImageView(   58):     at android.content.res.Resources.getDrawable(Resources.java:580)
W/ImageView(   58):     at android.widget.ImageView.resolveUri(ImageView.java:489)
W/ImageView(   58):     at android.widget.ImageView.onMeasure(ImageView.java:592)
W/ImageView(   58):     at android.view.View.measure(View.java:8171)
W/ImageView(   58):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
W/ImageView(   58):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
W/ImageView(   58):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:696)
W/ImageView(   58):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:306)
W/ImageView(   58):     at android.view.View.measure(View.java:8171)
W/ImageView(   58):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
W/ImageView(   58):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
W/ImageView(   58):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
W/ImageView(   58):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
W/ImageView(   58):     at android.view.View.measure(View.java:8171)
W/ImageView(   58):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
W/ImageView(   58):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
W/ImageView(   58):     at android.view.View.measure(View.java:8171)
W/ImageView(   58):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
W/ImageView(   58):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
W/ImageView(   58):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
W/ImageView(   58):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
W/ImageView(   58):     at android.view.View.measure(View.java:8171)
W/ImageView(   58):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
W/ImageView(   58):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
W/ImageView(   58):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
W/ImageView(   58):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
W/ImageView(   58):     at android.view.View.measure(View.java:8171)
W/ImageView(   58):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
W/ImageView(   58):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
W/ImageView(   58):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
W/ImageView(   58):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
W/ImageView(   58):     at android.view.View.measure(View.java:8171)
W/ImageView(   58):     at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:989)
W/ImageView(   58):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
W/ImageView(   58):     at android.widget.ScrollView.onMeasure(ScrollView.java:286)
W/ImageView(   58):     at android.view.View.measure(View.java:8171)
W/ImageView(   58):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
W/ImageView(   58):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
W/ImageView(   58):     at android.view.View.measure(View.java:8171)
W/ImageView(   58):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
W/ImageView(   58):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
W/ImageView(   58):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
W/ImageView(   58):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
W/ImageView(   58):     at android.view.View.measure(View.java:8171)
W/ImageView(   58):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
W/ImageView(   58):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
W/ImageView(   58):     at android.view.View.measure(View.java:8171)
W/ImageView(   58):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
W/ImageView(   58):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
W/ImageView(   58):     at android.view.View.measure(View.java:8171)
W/ImageView(   58):     at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
W/ImageView(   58):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
W/ImageView(   58):     at android.os.Handler.dispatchMessage(Handler.java:99)
W/ImageView(   58):     at android.os.Looper.loop(Looper.java:123)
W/ImageView(   58):     at com.android.server.ServerThread.run(SystemServer.java:506)

不知道我在哪里弄错了。我正在关注here的示例教程。

提前致谢。

1 个答案:

答案 0 :(得分:1)

问题在于int icon = 1;

您应该在那里添加一个真实的资源ID,因此它将显示在通知栏中。您获得Resources$NotFoundException,因为找不到标识为1的资源。

例如,如果您将名为xyz.png的图片放入res/drawable文件夹,则图片的ID将为R.drawable.xyz,可以从以下代码中调用:

int icon = R.drawable.xyz;