I'm new to android development, and I'm struggling with implementing a notification in my app. basically the problem is that the notification won't show up (and it did at the beginning), and I have no idea why, I've looked at other notification problems here and in other forums, but still didn't managed to fixed mine. It's getting quite frustrating and I'm posting it here as somewhat of a last resort 'cause I'm tired of guessing what's wrong... this is my code, I get no errors but the notification simply won't show. any help would be much appreciated.

Anyway, this is the code, I've declared the brodcastReciver in the manifest and everything.

Thanks, 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);

}

}


Update

Thanks a lot ! I'm using AlarmManager to call a notification after some time and this notification should open an activity. The Alarammanager seems to work, because when I try to call a different activity (instead of the notification), it opens it at the right time, but when I call the notification, nothing happens.

I'm having a few errors trying to implement your code, what am I doing wrong?

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

The logcat doesn't show anything, but the console gives this:

    [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)
有帮助吗?

解决方案

According to the documentation, you can try something like this

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());
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top