为什么NotificationManager无法接受我的通知

6

我无法让NotificationManagerNotify方法接受我的参数。该方法应该接受三个参数。

String        tag
int           id
Notification  notification

为构建通知,我使用的是NotificationCompat类,但我甚至尝试过Notification.Builder

在此输入图片描述

import android.app.Notification;
import android.support.v4.app.NotificationCompat;

关于我的构建配置,如下所示:

compileSdkVersion 23
buildToolsVersion "23.0.2"

minSdkVersion 14
targetSdkVersion 21

编辑:代码转录:

import android.app.Notification;
import android.support.v4.app.NotificationCompat;

private void showNotification(Context context, String category, String title, String text, int tag, boolean ongoing)
{
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, new Random().nextInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle(title);
    builder.setContentText(text);
    builder.setContentIntent(pendingIntent);
    builder.setOngoing(ongoing);
    builder.setSmallIcon(R.drawable.ic_launcher);

    Notification notification = builder.build();
    notificationManager.showNotification(context, category, title, text, new Random().nextInt(), ongoing);
    notificationManager.notify(null, 0, notification);
}

2
通知管理器的初始化在哪里? - Vivek Mishra
1
你是否正在使用 NotificationManagerCompat - Pedro Oliveira
2
你能把代码以文本形式发布,而不是图片吗? - Tim
我已经添加了代码转录。 - Martin Rončka
1
复制了你的代码,但无法重现问题。 - Tim
显示剩余2条评论
3个回答

6

使用Compat版本的NotificationManagerCompat,初始化它:notificationManager = NotificationManagerCompat.from(getApplicationContext());。之后,通知管理器:notificationManager.notify(null, 0, notification);

import android.support.v4.app.NotificationCompat;

...

private NotificationManagerCompat notificationManager;

...

// onCreate
notificationManager = NotificationManagerCompat.from(getApplicationContext());

...

// Somewhere in your code
Notification notification = new NotificationCompat.Builder(this)
    .setContentTitle("I'm a title")
    .setContentText("Some text")
    .setSmallIcon(R.drawable.ic_notification)
    .setContentIntent(pendingIntent)
    .build();

notificationManager.notify(null, 0, notification);

1
你正在使用NotificationManager,但是应该使用NotificationManagerCompat - Tom Sabel
确实,你说得对,我错过了那个,抱歉 :) 我是根据官方指南编写的。可能在引用普通的 NotificationManger 时出现了错误。http://developer.android.com/training/notify-user/build-notification.html - Martin Rončka
@MartinRončka 这可能不是真正的问题,还有其他情况发生了。否则它不会告诉你“Object中的notify()”,而是“NotificationManager中的notify()”。 - Tim
@Tim Castelijns,这个已经可以工作并且测试成功了。 - Tom Sabel
@Exaqt 我不是说它不起作用。我是说错误消息和建议的解决方案不匹配。如果这是解决方案,那么错误消息就没有意义。 - Tim
@Tim Castelijns 确实,错误消息表明notificationManager是对象类型。也许这是sdk版本问题? - Martin Rončka

3

Object中的notify

这个错误信息的部分提示您的notificationManager是一个Object,并且您是这样声明它的。

Object notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE);

你应该拥有的是这个。
NotificationManager notificationManager = (NotificationManager)
            getSystemService(Context.NOTIFICATION_SERVICE);

你抓得很好。 - Pedro Oliveira
我已经声明了类型为NotificationManager的notificationManager,很抱歉没有在图片中包含它。 - Martin Rončka

1

检查导入。这是我一直在使用的。

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

对于这个函数 -

private void createNotification(String idString, String title, String body, boolean isSound,
                                    boolean isVibration, PendingIntent intent) {
        Context context = getBaseContext();
        Bitmap mIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.styfi_largenotify);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.styfi_notify)
                .setColor(Color.TRANSPARENT)
                .setContentTitle(title)
                .setLargeIcon(mIcon)
                .setContentText(body)
                .setContentIntent(intent)
                .setLights(Color.CYAN, 3000, 3000)
                .setAutoCancel(true);

        if (isSound)
            mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        if (isVibration)
            mBuilder.setVibrate(new long[] { 500, 500});

        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        String actual = "";
        for (int i = 0; i < idString.length(); i++) {
            char c = idString.charAt(i);
            int value;
            try{
                value = Integer.parseInt(String.valueOf(c));
            } catch (Exception e) {
                value = Math.abs(c - 'a' + 1);
            }
            actual += String.valueOf(value);
            if (BuildConfig.DEBUG)
                Log.d(TAG, "char = [" + c + "], value = [" + value + "], actual = [" + actual + "]");
        }

        BigInteger idBigInt = new BigInteger(actual);
        int id = Math.abs(idBigInt.intValue());

        if (BuildConfig.DEBUG)
            Log.d(TAG, "idLong = [" + idBigInt +"], id = [" + id + "]");
        mNotificationManager.notify(id, mBuilder.build());
    }

问题已经解决,错误在于使用了旧的NotificationManagerNotificationCompat.Builder。现已更改为NotificationCompatManager - Martin Rončka

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接