为什么NotificationManager不接受我的通知

时间:2016-04-13 08:50:16

标签: android compiler-errors notifications

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

String        tag
int           id
Notification  notification

enter image description here

为了构建通知,我使用的是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);
}

3 个答案:

答案 0 :(得分:3)

使用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 :(得分:3)

  

在对象中通知

'error'消息的这一部分暗示你的notificationManager是一个Object,并且你声明它是这样的

Object notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE);

你应该拥有的是这个

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

答案 2 :(得分: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());
    }