点击通知不会启动特定活动

时间:2014-04-04 08:49:01

标签: android notifications action

我正在使用通知,其中添加了一个应该启动活动的“操作”。

我有两个主要活动:Login Activity和MainActivity。当LoginActivity启动时,它会自动登录用户(如果用户以前登录过),执行其他操作,然后启动MainActivity。 MainActivity是几个片段的主机。

现在我添加了这样的LoginActivity:

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

 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
         Intent.FLAG_ACTIVITY_SINGLE_TOP);
 PendingIntent intentp =
         PendingIntent.getActivity(getActivity(), 0, notificationIntent, 0);            

    NotificationCompat.Builder nBuilder;
    Uri alarmSound = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    nBuilder = new NotificationCompat.Builder(getActivity())
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Smart Share - " + "")
            .setLights(Color.BLUE, 500, 500).setContentText("message")
            .setAutoCancel(true).setTicker("Notification from smartshare")
            .setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })
            .setSound(alarmSound)
            .setAutoCancel(true)
            .setContentIntent(intentp);
    NotificationManager nNotifyMgr = (NotificationManager) getActivity()
            .getSystemService(getActivity().NOTIFICATION_SERVICE);
   Notification not = nBuilder.build();
   nNotifyMgr.notify(2 + 2, not);

一切正常。当我将MainActivity放入notificationIntent时,单击通知不会执行任何操作。

怎么回事?

修改

我的清单文件中的代码段:

  <activity
        android:name="<packagename>.LoginActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateVisible|adjustResize" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>  
    </activity>

    <activity
        android:name="<packagename>.MainActivity"
        android:label="@string/tab_title_mapview" 
        android:screenOrientation="portrait">
    </activity>

4 个答案:

答案 0 :(得分:3)

尝试这个家伙实际上你想念 manager.notify(100,通知);

NotificationManager mManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notification = new Notification(R.drawable.ic_launcher, "Tittle", System.currentTimeMillis());
                RemoteViews contentView = new RemoteViews(getBaseContext().getPackageName(), R.layout.customnotification_alert);
                contentView.setProgressBar(R.id.progressBar, 10, 0, false);        
                contentView.setTextViewText(R.id.text, "text ");       
                notification.contentView = contentView;

                Intent notificationIntent = new Intent(getBaseContext(), NotifyMessage.class);
                PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, 0);
                notification.contentIntent = contentIntent;
                mManager.notify(100, notification);

答案 1 :(得分:1)

试试这个:

PendingIntent contentIntent = PendingIntent.getActivity(getActivity(),0,PendingIntent.FLAG_UPDATE_CURRENT| PendingIntent.FLAG_ONE_SHOT);

答案 2 :(得分:0)

试试这个

    Notification notification = new Notification(icon, message, when);
 notificationIntent = new Intent(getActivity(), LoginActivity.class);
 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
     Intent.FLAG_ACTIVITY_SINGLE_TOP);
 PendingIntent intentp =
       PendingIntent.getActivity(getActivity(), 0, notificationIntent, 0);

notification.setLatestEventInfo(context, title, message, notificationIntent );

答案 3 :(得分:0)

出现同样的问题,我通过以下方式解决了这个问题:

将此添加到您的清单(活动代码内)

android:launchMode="singleTop"

并将以下标志设置为您要启动的意图:

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

希望它有所帮助!