Android打开应用程序触摸通知

时间:2015-12-15 13:41:16

标签: android

目前我在我的Android应用中设置了这样的通知:

private Notification getNotification(String content) {
    Notification.Builder builder = new Notification.Builder(this);
    builder.setContentTitle(getString(R.string.app_name));
    builder.setContentText(content);
    builder.setTicker(content);
    builder.setSmallIcon(R.drawable.ic_notification_appicon);
    builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    builder.setDefaults(Notification.DEFAULT_VIBRATE);
    builder.setAutoCancel(true);
    builder.setLights(Color.GREEN, 500, 500);
    return builder.getNotification();
}

问题是,我想按下应该打开应用程序的通知。但如果我触摸通知,则没有任何反应。 任何想法?

4 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

您需要将setContentIntent设置为通知构建器

Intent resultIntent = new Intent(this, YourActivity.class);
PendingIntent yourPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(yourPendingIntent);

您可以参考此tutorial了解详情。

答案 2 :(得分:0)

你可以在这里找到一个完整的例子:http://android-er.blogspot.bg/2013/06/start-activity-once-notification-clicked.html或尝试这样的事情:

 private static final int MY_NOTIFICATION_ID=1;

 Intent myIntent = new Intent(context, DoSomething.class);
 PendingIntent pendingIntent = PendingIntent.getActivity(
        context, 
        0, 
        myIntent, 
        Intent.FLAG_ACTIVITY_NEW_TASK);

 Notification myNotification = new NotificationCompat.Builder(context)
   .setContentTitle("Notification!")
   .setContentText("Do Something...")
   .setTicker("Notification!!!")
   .setWhen(System.currentTimeMillis())
   .setContentIntent(pendingIntent)
   .setDefaults(Notification.DEFAULT_SOUND)
   .setAutoCancel(true)
   .setSmallIcon(R.drawable.ic_launcher)
   .build();

 NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
 notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

答案 3 :(得分:0)

您需要在PendingIntent中添加builder.setContentIntent(pendingIntent)才能启动活动。

private Notification getNotification(String content) {
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle(getString(R.string.app_name));
builder.setContentText(content);
builder.setTicker(content);
builder.setSmallIcon(R.drawable.ic_notification_appicon);
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
builder.setDefaults(Notification.DEFAULT_VIBRATE);
builder.setAutoCancel(true);
builder.setLights(Color.GREEN, 500, 500);
// This intent is fired when notification is clicked
        Intent intent = new Intent(getApplicationContext(), YouMainActvity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        // Set the intent that will fire when the user taps the notification.
        builder.setContentIntent(pendingIntent);
 return builder.getNotification();
 }