2015-12-15 45 views
0

觸摸我設置在我的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(); 
} 

的問題是,我想按上應該打開應用程序的通知。但如果我觸及通知,則什麼都不會發生。 有什麼想法?

回答

0

您需要設置setContentIntent到您的通知建設者

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

您可以參考這個tutorial以獲取更多信息。

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); 
0

您需要在builder.setContentIntent(pendingIntent)添加PendingIntent開始actvity。

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(); 
}