Open application after clicking on Notification

后端 未结 11 2000
南笙
南笙 2020-11-22 13:52

I have a notification in my app with the following code:

//Notification Start

   notificationManager = (NotificationManager) getSystemService(Context.NOTIFI         


        
相关标签:
11条回答
  • 2020-11-22 14:49

    Thanks to above posts, here's the main lines - distilled from the longer code answers - that are necessary to connect a notification with click listener set to open some app Activity.

    private Notification getNotification(String messageText) {
    
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentText(messageText);
        // ...
    
        Intent appActivityIntent = new Intent(this, SomeAppActivity.class);
    
        PendingIntent contentAppActivityIntent =
                PendingIntent.getActivity(
                                this,  // calling from Activity
                                0,
                                appActivityIntent,
                                PendingIntent.FLAG_UPDATE_CURRENT);
    
        builder.setContentIntent(contentAppActivityIntent);
    
        return builder.build();
    }
    
    0 讨论(0)
  • 2020-11-22 14:51

    Please use below code for complete example of simple notification, in this code you can open application after clicking on Notification, it will solve your problem.

    public class AndroidNotifications extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            Button notificationButton = (Button) findViewById(R.id.notificationButton);
    
            notificationButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Timer timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            // Notification Title and Message
                            Notification("Dipak Keshariya (Android Developer)",
                                    "This is Message from Dipak Keshariya (Android Developer)");
                        }
                    }, 0);
                }
            });
        }
    
        // Notification Function
        private void Notification(String notificationTitle,
                String notificationMessage) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            android.app.Notification notification = new android.app.Notification(
                    R.drawable.ic_launcher, "Message from Dipak Keshariya! (Android Developer)",
                    System.currentTimeMillis());
    
            Intent notificationIntent = new Intent(this, AndroidNotifications.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                    notificationIntent, 0);
    
            notification.setLatestEventInfo(AndroidNotifications.this,
                    notificationTitle, notificationMessage, pendingIntent);
            notificationManager.notify(10001, notification);
        }
    }
    

    And see below link for more information.

    Simple Notification Example

    0 讨论(0)
  • 2020-11-22 14:53

    Looks like you missed this part,

    notification.contentIntent = pendingIntent;
    

    Try adding this and it should work.

    0 讨论(0)
  • 2020-11-22 14:54

    This is the way that I have approached.

    public class AppFCMService extends FirebaseMessagingService {
    
        private final static String TAG = "FCM Message";
        String notify, requstId, Notification;
        PendingIntent pendingIntent;
    
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            Log.d(TAG, "From: " + remoteMessage.getFrom());
            Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
            //split string and getting order id from notification
            String Str = remoteMessage.getNotification().getBody();
            String[] tmp;
            tmp = Str.split(" ");
            String temp1 = tmp[0];
            String temp2 = tmp[1];
            String id = tmp[2];
            notify = temp1 + " " + temp2;
            requstId = id;
            showNotification(remoteMessage.getNotification().getBody());
        }
    
        private void showNotification(String messageBody) {
            // check whether session has been initiated or not
            if (new SessionHelper(getApplicationContext()).isLoggedIn()) {
    
                if (notify.equalsIgnoreCase("Travel request")) {
    
                    Intent intent = new Intent(this, ViewTravelDetailsActivity.class);
                    intent.putExtra("TravelRequestID", requstId);
                    intent.putExtra("BackPress", "Notify");
                    pendingIntent = PendingIntent.getActivity(this, 0, intent,
                            PendingIntent.FLAG_ONE_SHOT);
    
                } else if (notify.equalsIgnoreCase("Timesheet replied")) {
    
                    Intent intent = new Intent(this, ViewReplyActivity.class);
                    intent.putExtra("timesheetActivityID", requstId);
                    intent.putExtra("BackPress", "Notify");
                    intent.putExtra("RealmData", "DeleteRealm");
                    intent.putExtra("isToday", "true");
                    pendingIntent = PendingIntent.getActivity(this, 0, intent,
                            PendingIntent.FLAG_ONE_SHOT);
                } else {
    
                    Intent intent = new Intent(this, Dashboard.class);
                    intent.putExtra("timesheetActivityID", requstId);
                    pendingIntent = PendingIntent.getActivity(this, 0, intent,
                            PendingIntent.FLAG_ONE_SHOT);
                }
            } else {
    
                Intent intent = new Intent(this, LoginActivity.class);
                intent.putExtra("timesheetActivityID", requstId);
                pendingIntent = PendingIntent.getActivity(this, 0, intent,
                        PendingIntent.FLAG_ONE_SHOT);
            }
    
            Bitmap notifyImage = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(notifyImage)
                    .setColor(Color.parseColor("#FFE74C3C"))
                    .setContentTitle("TEST")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notificationBuilder.build());
        }
    }
    
    0 讨论(0)
  • 2020-11-22 14:58

    Here's example using NotificationCompact.Builder class which is the recent version to build notification.

    private void startNotification() {
        Log.i("NextActivity", "startNotification");
    
     // Sets an ID for the notification
          int mNotificationId = 001;
    
        // Build Notification , setOngoing keeps the notification always in status bar
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ldb)
                        .setContentTitle("Stop LDB")
                        .setContentText("Click to stop LDB")
                        .setOngoing(true);
    
        // Create pending intent, mention the Activity which needs to be 
        //triggered when user clicks on notification(StopScript.class in this case)
    
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, StopScript.class), PendingIntent.FLAG_UPDATE_CURRENT);
    
    
        mBuilder.setContentIntent(contentIntent);
    
    
        // Gets an instance of the NotificationManager service
       NotificationManager mNotificationManager =
                (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    
    
        // Builds the notification and issues it.
        mNotificationManager.notify(mNotificationId, mBuilder.build());
    
    
    }
    
    0 讨论(0)
提交回复
热议问题