点击通知无法启动活动

时间:2014-04-01 18:02:25

标签: android service notifications

我正在从服务创建通知;显示通知,但是当我点击它时,没有任何反应:它应该打开一个活动。

我的代码:

NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, "test", when);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |   Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(this, 0, 
notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);

notification.setLatestEventInfo(this, "title", "message", intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);

但是,如果我在活动中使用几乎相同的代码,我可以点击通知,然后显示我的活动。我做错了什么?

修改 事实证明这个代码没有任何问题,有一个不同的问题: 当我的服务完成后,它使用上面的代码创建了通知。但是,该服务还广播了它已完成,并且接收者创建了另一个通知,该通知使用不同的代码来创建通知(没有PendingIntents,因此在单击通知时没有定义的操作),并且该通知必须放置自己而不是原来的,正确的。

4 个答案:

答案 0 :(得分:3)

除了在评论中使用@Raghunandan建议的支持库v4中使用Notification.Builder以及支持库v4中的NotificationCompat.Builder之外,我遇到了与您的问题可能的常见解决方案相同的问题。

这具体针对4.4,如此处所示:Issue 63236:Notification with TaskStackBuilder.getPendingIntent() is not open the Activity和此处Issue 61850: KitKat notification action Pending Intent fails after application re-install

一个确认的解决方案是对与您要创建的PendingIntent相同的PendingIntent执行cancel()操作。

对我来说有用的是修改目标Activity的清单定义并添加 android:exported =" true" "活动"目标Activity的标签。那就是我假设的MainActivity。

示例:

<activity
        android:name="com.your.MainActivity"
        android:exported="true" >
.
.
</activity>

答案 1 :(得分:1)

这适用于api级别8.

   private int NOTIFICATION_ID = 1;
    private Notification mNotification;
    private NotificationManager mNotificationManager;
    private PendingIntent mContentIntent;
    private CharSequence mContentTitle;

你可以像这样创建通知:

     mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

            //create the notification
            int icon =  R.drawable.ic_launcher;
            CharSequence tickerText = mContext.getString(R.string.noti_comes); //Initial text that appears in the status bar

            long when =  System.currentTimeMillis();
            mNotification = new Notification(icon, tickerText, when);

            //create the content which is shown in the notification pulldown
            mContentTitle = mContext.getString(R.string.noti_comes_t); //Full title of the notification in the pull down
            CharSequence contentText = clck_see_noti; //Text of the notification in the pull down
            //you can set your click event to go to activity
            mContentIntent  = PendingIntent.getActivity(mContext, 0, new Intent(mContext, MainActivity.class), 0); 

            //add the additional content and intent to the notification
            mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, mContentIntent);

            //make this notification appear in the 'Ongoing events' section
            mNotification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL ;

            //show the notification
            mNotificationManager.notify(NOTIFICATION_ID, mNotification);

并且不要忘记您的服务正在清单中注册

 <service
        android:name="com.xx.your_service" 
        android:enabled="true"  
         >
    </service>

答案 2 :(得分:0)

如果您定义的Activity未注册到AndroidManifest.xml,则通知不会显示任何错误消息,并且不会发生。

Intent notificationIntent = new Intent(this, MainActivity.class);

请务必让Activity从注册到AndroidManifest.xml的通知开始

这家伙有类似的问题“我在清单中拼错了我的活动名称。”:

<强> launch activity from service when notification is clicked

答案 3 :(得分:0)

我的情况是我的项目使用Android Annotations。所以我的错误是当我创建Intent时,我总是这样设置:

Intent notificationIntent = new Intent(this, MainActivity.class);

但是必须设置Android Annotation的生成类:

Intent notificationIntent = new Intent(this, MainActivity_.class);

这解决了我的问题