使用通知管理器和notification.builder通知无效

时间:2013-06-25 09:44:48

标签: android notifications broadcastreceiver notificationmanager

我是Android开发的新手,我正在努力在我的应用程序中实现通知。 基本上问题是通知不会显示(并且它在开始时显示),我不知道为什么,我在这里和其他论坛中查看了其他通知问题,但仍未设法修复矿。 它变得非常令人沮丧,我在这里发布它作为最后的手段因为我厌倦了猜测什么是错的... 这是我的代码,我没有错误,但通知根本不会显示。 任何帮助将不胜感激。

无论如何,这是代码,我已经在清单中声明了brodcastReciver以及所有内容。

谢谢,Elad

public class NotificationService extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, SplashTest.class), 0);
    int mNotificationId = 001;

    Notification notification = new Notification.Builder(context)

            .setContentTitle("Title")
            .setContentText("Text")
            .setSmallIcon(R.drawable.notification_icon)
            .setContentIntent(contentIntent)
            .build();

    notificationManager.notify(mNotificationId, notification);

}

}


更新

非常感谢! 我使用AlarmManager在一段时间后调用通知,此通知应该打开一个活动。 Alarammanager似乎有效,因为当我尝试调用不同的活动(而不是通知)时,它会在正确的时间打开它,但是当我调用通知时,没有任何反应。

我在尝试实现代码时遇到了一些错误,我做错了什么?

    public class NewNotificationService extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            int mNotificationId = 0015;
            int icon = R.drawable.notification_icon;

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!");

Intent resultIntent = new Intent(this, SplashTest.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(SplashTest.class); 
stackBuilder.addNextIntent(resultIntent);

PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                                    0, PendingIntent.FLAG_UPDATE_CURRENT );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(0, mBuilder.build());

logcat没有显示任何内容,但是控制台提供了这个:

    [2013-06-25 23:45:30 - ddmlib] Broken pipe
java.io.IOException: Broken pipe
    at sun.nio.ch.FileDispatcher.write0(Native Method)
    at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:29)
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:69)
    at sun.nio.ch.IOUtil.write(IOUtil.java:40)
    at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:336)
    at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
    at com.android.ddmlib.Client.sendAndConsume(Client.java:607)
    at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:348)
    at com.android.ddmlib.Client.requestAllocationStatus(Client.java:453)
    at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:835)
    at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:803)
    at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:763)
    at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:652)
    at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:44)
    at com.android.ddmlib.DeviceMonitor$3.run(DeviceMonitor.java:580)

1 个答案:

答案 0 :(得分:3)

According to the documentation,您可以尝试这样的事情

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!");

Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class); 
stackBuilder.addNextIntent(resultIntent);

PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                                    0, PendingIntent.FLAG_UPDATE_CURRENT );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(mId, mBuilder.build());