Android 通知不起作用 [英] Android notification not working

查看:30
本文介绍了Android 通知不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一整天都在尝试从 ASyncTask 获得成功上传的通知.我当前的代码没有收到任何错误,但我无法在通知栏(或其他任何地方)中显示通知.我在 LogCat 中没有收到任何消息,通知栏中也没有出现任何通知.这是我的代码:

I've been attempting to get a notification of a successful upload from an ASyncTask to work all day. I'm not getting any errors from my current code but I can't get the notification to show in the notification bar (or anywhere else). I get no messages in LogCat and no notification appears in the Notification bar. This is my code:

Notification mNotification = new Notification(icon, tickerText, when);

CharSequence contentTitle = "upload completed.";
CharSequence contentText = "upload completed.";

Intent notificationIntent = new Intent(context, CastrActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_NO_CREATE);
mNotification.contentIntent = contentIntent;
mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);

这是从 ASyncTask 的 onPostExecute() 方法调用的.老实说,我对 PendingIntent 部分有点困惑.对我怀疑是不正确代码的任何澄清将不胜感激.

This is called from the onPostExecute() method of an ASyncTask. I'm a bit confused on the PendingIntent part, to be honest. Any clarification of what I suspect to be incorrect code there would be greatly appreciated.

推荐答案

我创建了显示通知的类:

I have created the class to show notifications:

public class NotificationData {

    public static NotificationManager mNotificationManager;
    public static int SIMPLE_NOTFICATION_ID;
    private Context _context;

    public NotificationData(Context context) {
        _context = context;
    }

    public void clearNotification() {
        mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
    }

    public void SetNotification(int drawable, String msg, String action_string, Class cls) {
        mNotificationManager = (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE);
        final Notification notifyDetails = new Notification(drawable, "Post Timer", System.currentTimeMillis());
        long[] vibrate = { 100, 100, 200, 300 };
        notifyDetails.vibrate = vibrate;
        notifyDetails.ledARGB = 0xff00ff00;
        notifyDetails.ledOnMS = 300;
        notifyDetails.ledOffMS = 1000;
     // notifyDetails.number=4;
        notifyDetails.defaults =Notification.DEFAULT_ALL;
        Context context = _context;
        CharSequence contentTitle = msg;
        CharSequence contentText = action_string;      
        Intent notifyIntent = new Intent(context,  cls);
     // Bundle bundle = new Bundle();
     // bundle.putBoolean(AppConfig.IS_NOTIFICATION, true);
        notifyIntent.putExtras(bundle);
        PendingIntent intent = PendingIntent.getActivity(_context, 0,notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
        notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
        mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);        
    }
}

如何使用这个类:

NotificationData notification; //create object
notification = new NotificationData(this);
notification.SetNotification(R.drawable.notification, "Notification Title", "Click to open", YourClassName.class);

添加权限android.permission.VIBRATE

这篇关于Android 通知不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆