Android 未决意图不起作用

Android 未决意图不起作用,android,push-notification,android-pendingintent,Android,Push Notification,Android Pendingintent,我知道我问的问题是重复的。但是我尝试过各种各样的事情。我只想在单击通知时调用MainActivity类。我的密码是 private void sendNotification(String msg) { Log.d(TAG, "Preparing to send notification...: " + msg); int requestID = (int) System.currentTimeMillis(); mNotificationManager = (Noti

我知道我问的问题是重复的。但是我尝试过各种各样的事情。我只想在单击通知时调用MainActivity类。我的密码是

private void sendNotification(String msg) 
{
    Log.d(TAG, "Preparing to send notification...: " + msg);
    int requestID = (int) System.currentTimeMillis();
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent notificationIntent = new Intent(this,MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                                            .setSmallIcon(R.drawable.icon)
                                            .setContentTitle("Hashtag")
                                            .setContentText(msg)
                                            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                                            .setContentIntent(contentIntent);
    mBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
    mBuilder.setLights(Color.RED, 3000, 3000);
    mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
    mBuilder.setAutoCancel(true);

    mNotificationManager.notify((int)Calendar.getInstance().getTimeInMillis(), mBuilder.build());
    Log.e("NEW NOTIFICATION ID IS --- ", String.valueOf((int)Calendar.getInstance().getTimeInMillis()));
    Log.d(TAG, "Notification sent successfully.");
}
我的主要活动课程如下:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    loadUrl(launchUrl);
    Toast.makeText(getApplicationContext(), "this is my Toast!!!",Toast.LENGTH_LONG).show();
}

我希望大家都来祝酒。请大家。。帮帮我!!这一整天都在折磨我

要显示吐司,请在下面一行使用

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
代替

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

您的祝酒词不会激发,因为由于SIngleTop,该活动没有被重新创建。它将重用现有的活动。请使用标志\活动\新任务,而不是单顶,然后将显示该活动。@e4c5 Ohh。。有没有其他方法来获得祝酒辞?不太确定你是否应该按照给出的答案中的建议使用新任务标志。您可以通过将代码移动到onResume()方法中来获得toast。这将解决问题,但也可能不理想,这完全取决于你的活动内容(我们不知道)。我想应该读一读活动生命周期。@e4c5当我点击通知时,我想用javascript调用一个函数。那是我的需要。试过了。。不变。。还是一样