小部件不显示通知

时间:2014-09-11 20:10:20

标签: android android-widget android-notifications

我想在小部件上通知通知。但它没有显示它

我调用此函数但在模拟器android 4.3中调用此函数时只播放声音。它没有显示任何通知

void DisplayZiyaratPage()
        {

            NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this)
                .setContentTitle("Title")
                .setContentText("text")
                .setSound(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.alarm_sound))
                .setDefaults(Notification.DEFAULT_VIBRATE);
            Intent intent = new Intent(this, ZiyaratActivity.class);

            PendingIntent pendingIntent =PendingIntent.getActivity(this, 0, intent, 0);
            notificationBuilder.setContentIntent(pendingIntent);
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notificationBuilder.build());
        }

1 个答案:

答案 0 :(得分:0)

试试这个

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());

NotificationCompat.Builder类已添加到支持包中,因此我们可以使用它来支持API级别v4及更高版本:

http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html

如果您的应用支持的API版本与API级别4一样旧,则可以使用Android支持库中提供的NotificationCompat.Builder。