Android - 通知没有出现

时间:2015-10-21 11:24:52

标签: java android

我尝试使用Fragment中的AppCompat创建通知。 我创造什么都看不见。

代码:

private void createNotification()
{
    Context context = getActivity().getApplicationContext();
    Intent notificationIntent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.ymk_ya_logo)
            .setAutoCancel(false)
            .setContentTitle("notify")
            .setContentText("test");

    Notification notification = builder.build();

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(777, notification);
}

1 个答案:

答案 0 :(得分:0)

请尝试将最后一位更改为:

NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0, builder.build());