简体   繁体   中英

Application is not opening after clicking on Notification

NotificationManager notificationManager = (NotificationManager) CONTEXT
                            .getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = new Notification(R.drawable.ic_launcher,                                    "New Messege", System.currentTimeMillis());                         

// Hide the notification after its selected

notification.flags |= Notification.FLAG_AUTO_CANCEL;

// Opening application on notification select

Intent intent = new Intent(CONTEXT, Welcome.class);

//intent.putExtra("payload", payload);


PendingIntent pendingIntent = PendingIntent.getActivity(CONTEXT, 0,intent, 0);

notification.setLatestEventInfo(CONTEXT, "Message","message", pendingIntent);

notification.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(0, notification);

My problem is that, when I click on notification in Notification Center, it dose not start my app. Basically, after clicking on my notification nothing happens!I have tried lots of solution but nothing happen.

I have an another problem when the app is running(not in background) it also shows the notification in the notification bar.please guide me. Thanx in advance.

Have you tried to do :

notification.setContentIntent(pendingIntent);

Take a look at this brilliant tutorial, explaining a lot of this on notifications : http://www.vogella.com/tutorials/AndroidNotifications/article.html

Hope this code snippnet helps you:

    @SuppressLint("NewApi")
    public void shownotification(Context context)
    {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My notification")
                .setContentText("Hello World!")
               // .setDeleteIntent(getDeleteIntent())
               // .setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0))
                .setOngoing(true);

        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(getApplicationContext(), MYDEMOACTIVITY.class);
            // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(MYDEMOACTIVITY.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // mId allows you to update the notification later on.
        mNotificationManager.notify(mId, mBuilder.build());
    }

Explaination: Here mid is my app's notification id eg:final int mId = 0; and MYDEMOACTIVITY.class is my another activity which I want to start after clicking the notification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM