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 に答える