2015-12-15 10 views
0

в данный момент я установить уведомление в моем приложение для 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

Вам нужно добавить PendingIntent в builder.setContentIntent(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(); 
} 
Смежные вопросы