Kitkat的通知

时间:2014-02-25 10:22:35

标签: android android-notifications

我在点击时创建通知我要求取消服务。它在4.4(Kitkat)以下工作正常。我的应用程序支持2.2(API 8以后)

On Kitkit(Nexus 5)根本不调用该服务。我不是我在哪里错了?即使在4.3版本上它也能正常工作?

以下是我在除Nexus 5(Kitkat)之外的每部手机上尝试过的工作

createNotification("Click here to cancel notification!", "TestApp");

我在致电特定服务后立即致电上述人员。

private void createNotification(String body,String title) 
{
    NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    int unique_id = 007;

    Intent nintent = new Intent(this,ServicetocancelAlarm.class);
    PendingIntent pin = PendingIntent.getService(this,0, nintent, 0);

    //set notify image
    Notification n = new Notification(R.drawable.ic_launcher, body,java.lang.System.currentTimeMillis());

    n.contentIntent = pin;
    n.setLatestEventInfo(this, title, body, pin);

    n.defaults = Notification.DEFAULT_ALL;
    nm.notify(unique_id, n);
}

有人可以帮我解决KITKAT这个问题吗?

更新

我尝试了以下内容:

   Notification("Message", "This is Android Notification Message");

和方法

   @SuppressWarnings("deprecation")
private void Notification(String notificationTitle, String notificationMessage)
{
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher, "A New Message",
            System.currentTimeMillis());

    Intent notificationIntent = new Intent(this, TransparentActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(MainActivity.this, notificationTitle, notificationMessage, pendingIntent);
    notificationManager.notify(10001, notification);
}

这段代码在调试模式下工作正常但是如果导出apk并安装它。它根本不起作用。它没有进入新的活动。我不确定这里有什么问题。

1 个答案:

答案 0 :(得分:1)

我通过尝试以下代码解决了这个问题:

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= 16)
        {
            Context context = RepeatService.this;

            Intent notificationIntent = new Intent(context, ServicetocancelAlarm.class);
            PendingIntent contentIntent = PendingIntent.getService(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

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

            Resources res = context.getResources();
            Notification.Builder builder = new Notification.Builder(context);

            builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
            .setTicker(res.getString(R.string.ticker))
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentTitle(res.getString(R.string.app_name))
            .setContentText(res.getString(R.string.cancelText));
            Notification n = builder.build();

            nm.notify(007, n);
        }

其他部分有通常的通知代码,在API 16下工作正常。

尝试一下!!