【问题标题】:When user logged out and the push notification come my app opens automatically. i want to open my app on the click of push notification?当用户注销并且推送通知来时,我的应用程序会自动打开。我想在点击推送通知时打开我的应用程序?
【发布时间】:2016-09-30 12:04:33
【问题描述】:

这是我发送推送通知的方法:-

 private void sendNotification(String msg) {

        mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent contentIntent;
        Intent intentblank = new Intent(this, LoginActivity.class);
        intentblank.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intentblank);


        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        Log.e("message.....", msg);


        if (msg.equals("You Have got New Message")) {

            Log.e("msg occuring..", "intent enter in message...");
            intent.putExtra("KEYMESSAGE", "Message");

        } else {

            Log.e("notification occuring..", "notification occurs.....");
            intent.putExtra("KEYNOTIFICATION", "aman");


        }



        if (sharedPreferencesUtilities.getEmail().equals("") || sharedPreferencesUtilities.getPassword().equals("")) {
            Log.e("blank notification....", "blank notification...........................");
            contentIntent  =PendingIntent.getActivity(this, 0, intentblank, 0);
        } else {
           contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        }


        mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(getNotificationIcon())
                .setContentTitle("Telepoh")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);

        mBuilder.setContentIntent(contentIntent);
        mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
        mBuilder.getNotification().flags |= Notification.FLAG_ONGOING_EVENT;
        mBuilder.setOngoing(false);

        mBuilder.setAutoCancel(true);


        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());


    }

    private int getNotificationIcon() {
        boolean useWhiteIcon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
        return useWhiteIcon ? R.drawable.push_icon : R.drawable.push_icon;
    }
}

我希望当用户注销时点击推送通知,登录页面会打开我的应用程序。但在我的情况下,如果用户注销,则应用程序会自动打开。我想点击推送通知。

【问题讨论】:

    标签: android push-notification google-cloud-messaging android-pendingintent


    【解决方案1】:

    删除下面的代码行

     startActivity(intentblank);
    

    【讨论】:

      【解决方案2】:

      始终在点击通知时调用登录活动。在登录活动中检查用户是否登录。如果已登录,则转到主要活动,否则继续登录活动

      【讨论】:

        【解决方案3】:

        使用这个:

                Intent loginIntent = new Intent(getApplicationContext(),LoginActivity.class);
                loginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                PendingIntent contentIntent = PendingIntent.getActivity(this, 0, loginIntent ,PendingIntent.FLAG_UPDATE_CURRENT);
        

        并将您的startActivity(loginIntent ); 放入您的登录检查条件中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-26
          相关资源
          最近更新 更多