Clicking on Notification is not starting intended activity?

不羁岁月 提交于 2019-11-27 04:36:39

My problem got solved I just have to add PendingIntent.FLAG_ONE_SHOT flag as well , so I replaced :

PendingIntent contentIntent = stackBuilder
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

to

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

I encountered the same issue and resolved it by adding android:exported="true" to the activity declaration in AndroidManifest.xml.

Here you just passed your Intent into pendingintent: see below

Intent notificationIntent = new Intent(context, Login.class);

 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
and set this contentintent into your Notification:

Notification noti = new NotificationCompat.Builder(context)
                    .setSmallIcon(icon_small)
                    .setTicker(message)
                    .setLargeIcon(largeIcon)
                    .setWhen(System.currentTimeMillis())
                    .setContentTitle(title)
                    .setContentText(message)
                    .setContentIntent(**contentIntent**)
                    .setAutoCancel(true).build();

This may help you.

if you launch the intended activity using Action String dont forget to add

<intent-filter>
       <action android:name="YOUR ACTION STRING"/>
       <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

inside <activity></activity> tag

The activity that you want to launch has to be designated as a LAUNCHER activity in your manifest - otherwise it won't launch via a Pending Intent. Add the following to your in the AndroidManifext.xml

<activity
...
android:exported="true">
<intent-filter>
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Otherwise you will need to use an Activity that is already designated as a LAUNCHER (such as your MAIN activity)

Do some thing like this on generateNotification() method ..

Replace your activity with Splash.Java class in it.

/**
     * Issues a notification to inform the user that server has sent a message.
     */
    @SuppressWarnings("deprecation")
    private static void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        //message = "vivek";
       // Log.d("anjan", message.split("~")[0]);
        //Toast.makeText(context, message, Toast.LENGTH_LONG).show();

        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = context.getString(R.string.app_name);
        Log.d("anjan1", title);
        String text_message = context.getString(R.string.title_activity_main);
        Log.d("anjan1", text_message);

        Intent notificationIntent = new Intent(context, Splash.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);      

    }

Try this instead of the last line :

mNotificationManager.notify(0, mBuilder.getNotification());

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!