【问题标题】:Why is NotificationManager not accepting my notification为什么 NotificationManager 不接受我的通知
【发布时间】:2016-04-13 08:50:16
【问题描述】:

我无法让NotificationManagerNotifiy 接受我的论点。该方法应该采用 3 个参数。

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);
}

【问题讨论】:

  • notificationManager 初始化在哪里?
  • 你在使用NotificationManagerCompat吗?
  • 你能把代码发布为文本而不是图像吗
  • 我已经添加了代码副本。
  • 复制了你的代码,但我无法重现问题

标签: android compiler-errors notifications


【解决方案1】:

使用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);

【讨论】:

  • 您使用的是NotificationManager ,但您应该使用NotificationManagerCompat
  • 确实,你是对的,我错过了,对不起 :) 我是根据官方指南写的。引用正常NotificationMangerdeveloper.android.com/training/notify-user/…时可能出现错误
  • @MartinRončka 这可能不是真正的问题,这里发生了其他事情。否则它不会告诉你notify() in Object,而是notify() in NotificationManager
  • @Tim Castelijns,这是有效的,并且经过测试可以正常工作。
  • @Exaqt 我并不是说它不起作用。我是说错误消息和建议的解决方案不匹配。如果这是解决方案,则错误消息毫无意义
【解决方案2】:

在对象中通知

“错误”消息的这一部分暗示您的 notificationManager 是一个对象,并且您这样声明它

Object notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE);

你应该拥有的是这个

NotificationManager notificationManager = (NotificationManager)
            getSystemService(Context.NOTIFICATION_SERVICE);

【讨论】:

  • 很好理解
  • 我确实声明了 NotificationManager 类型的 NotificationManager,很抱歉没有在图片中包含它。
【解决方案3】:

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

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());
    }

【讨论】:

  • 问题已经解决,错误是使用NotificationCompat.Builder 和旧的NotificationManager。将其修复为NotificationCompatManager
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
  • 2016-07-09
相关资源
最近更新 更多