1

这是我在单击按钮时调用的方法。

public void onDisplayNotification(View v)
{
    Intent i = new Intent(this, Notification_Activity.class);
    i.putExtra("Code", "Notification Dismissed");
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);
    NotificationCompat.Builder nb = new NotificationCompat.Builder(this);
    nb.setContentTitle("Meeting").setContentText("In 5 minutes").setContentIntent(pendingIntent);
    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Log.d("my","Hello");
    nm.notify(0, nb.build());
}

单击按钮时不显示任何内容。我检查了日志猫,该方法正在运行。

4

4 回答 4

2

请注意android 开发者指南

所需的通知内容

Notification 对象必须包含以下内容:

A small icon, set by setSmallIcon()
A title, set by setContentTitle()
Detail text, set by setContentText()

尝试为通知生成器提供小图标资源。

于 2013-08-09T09:32:17.317 回答
1

使用这样的东西:

public void createNotification(View view) 
{
    // Prepare intent which is triggered if the
    // notification is selected
    Intent intent = new Intent(this, NotificationReceiverActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

    // Build notification
    // Actions are just fake
    Notification noti = new Notification.Builder(this)
        .setContentTitle("New mail from " + "test@gmail.com")
        .setContentText("Subject").setSmallIcon(R.drawable.icon)
        .setContentIntent(pIntent)
        .addAction(R.drawable.icon, "Call", pIntent)
        .addAction(R.drawable.icon, "More", pIntent)
        .addAction(R.drawable.icon, "And more", pIntent).build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Hide the notification after its selected
    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, noti);
}
于 2013-08-09T08:54:22.690 回答
1

int NOTIFICATION_ID=1

    NotificationManager mNM = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
                    CharSequence NotificationTicket = "Test";
        CharSequence NotificationTitle = "Test";
        // specify the notification icon and time


        Notification notification = new Notification(R.drawable.icon,
                NotificationTicket, System.currentTimeMillis());
        Intent notificationIntent = new Intent();
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(context, NotificationTitle,
                NotificationContent, contentIntent);
        notification.flags |= Notification.FLAG_SHOW_LIGHTS
                | Notification.FLAG_AUTO_CANCEL;

        mNM.notify(NOTIFICATION_ID, notification);
于 2013-08-09T09:00:03.107 回答
-1

好吧,那种nb.build()类型的方法对我不起作用。我上个月这样尝试过,效果很好:

   NotificationCompat.Builder builder = (new NotificationCompat.Builder(this)
            .setContentTitle("Hello Notification").setContentText("Notification Time!")
            .setSmallIcon(R.drawable.ic_launcher).setContentIntent("your pending intent").setWhen(System.currentTimeMillis()));

   Notification notif = builder.getNotification();
        notif.defaults |= Notification.DEFAULT_SOUND;
        notif.flags |= Notification.FLAG_AUTO_CANCEL;
        notif.flags |= Notification.FLAG_SHOW_LIGHTS;

   NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify("put any id", notif);
        stopSelf();
于 2013-08-09T09:48:48.100 回答