2017-05-29 35 views
1

你好,我有以下功能,显示在我的应用程序的通知,但我突然开始有一个问题,当我点击一个通知它不会带我到我指定的活动.setContentIntent(),这是我的代码:待定意图不工作,通知不会打开活动

@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) 
private void getNotifications() { 
    ArrayList<com.xxxxxx.app234929.models.Notification> notifications = mNotificationsTable.get(); 

    int requestId = (int) System.currentTimeMillis(); 
    Intent notificationsIntent = new Intent(getApplicationContext(), NotificationsActivity.class); 
    notificationsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), requestId, 
      notificationsIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.InboxStyle inbox = new NotificationCompat.InboxStyle(new NotificationCompat.Builder(getApplicationContext()) 
      .setSmallIcon(R.drawable.ic_stat_mtgh_notification) 
      .setColor(getResources().getColor(R.color.colorPrimary)) 
      .setContentTitle("xxxxxx") 
      .setContentText("You have "+(notifications.size() > 1 ? "new updates" :"a new update")) 
      .setNumber(notifications.size()) 
      .setContentIntent(intent) 
      .setDefaults(Notification.DEFAULT_VIBRATE | 
        Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)) 
      .setBigContentTitle("xxxxxx"); 

    for (int i=0; i < notifications.size(); i++) { 
     inbox.addLine(notifications.get(i).getMessage()); 
    }; 

    Notification notification = inbox.build(); 

    NotificationManager notificationManager = (NotificationManager) getApplicationContext() 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(1, notification); 
} 

我GOOGLE了很多的解决方案,并试图他们,但他们没有工作对我来说,现在我不知道我在做什么错在这里。

+0

我改变了他的活动的名称和它的工作,改变了它回来了,它没有工作,所以我会说这只是其中的一件事,稍后会通过我的代码看看真正的问题是什么。 – user3718908

回答

0

这是我使用的代码和完美的作品,我有它的服务,但你可以在任何活动中使用:

Intent myIntent = new Intent(MyActivity.this, ActivityIWantToOpenOnClick.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(MyActivity.this, 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 

NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
NotificationCompat.Builder ncomp = new NotificationCompat.Builder(MyActivity.this); 
ncomp.setContentTitle("Message Tittle"); 
ncomp.setContentText("Message Text"); 
ncomp.setTicker("Message Ticker");         
ncomp.setSmallIcon(R.drawable.your_icon); 
ncomp.setAutoCancel(true); 
ncomp.setContentIntent(pendingIntent); 
nManager.notify((int) System.currentTimeMillis(), ncomp.build());