Clicking on Notification is not starting intended activity?

后端 未结 7 1709
野性不改
野性不改 2020-11-30 09:32

I am using GCM in my application and also using NotificationManager to Create a Notification whenever GCM message is received.Till now everything is working perfectly and GC

相关标签:
7条回答
  • 2020-11-30 10:14

    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);      
    
        }
    
    0 讨论(0)
  • 2020-11-30 10:17

    Try this instead of the last line :

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

    0 讨论(0)
  • 2020-11-30 10:19

    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);
    
    0 讨论(0)
  • 2020-11-30 10:24

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

    0 讨论(0)
  • 2020-11-30 10:25

    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

    0 讨论(0)
  • 2020-11-30 10:25

    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)

    0 讨论(0)
提交回复
热议问题