gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:43:40 27 4
gpt4 key购买 nike

我是 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 中的示例教程进行操作.

提前致谢。

最佳答案

问题出在 int icon = 1; 上。

你应该在那里添加一个真实的资源ID,这样它就会显示在通知栏中。您收到 Resources$NotFoundException,因为没有找到 ID 为 1 的资源。

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

int icon = R.drawable.xyz;

关于android - 通知管理器不显示任何类型的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15338421/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com