【问题标题】:Pending Intent not working待定意图不起作用
【发布时间】:2015-09-10 10:14:02
【问题描述】:

我知道我要问的问题是重复的。但我已经尝试了各种各样的事情。我想要的是,当我单击通知时调用 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.");
}

我的MainActivity类如下,

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

我想要调用 toast。请人们..帮帮我!这已经害死我一整天了..!!!

【问题讨论】:

  • 您的 toast 不会触发,因为由于 SIngleTop 而没有重新创建该活动。它将重用现有的。
  • 代替SingleTop,使用FLAG_ACTIVITY_NEW_TASK,就会显示activity。
  • @e4c5 Ohh.. 有没有其他方法可以得到吐司?
  • 不太确定您是否应该按照给出的答案中的建议使用新任务标志。您可以通过将该代码移至 onResume() 方法来获得祝酒词。它会解决问题,但这可能也不理想,这完全取决于您的活动是什么(我们不知道)。我认为对活动生命周期进行一些阅读是有必要的。
  • @e4c5 当我点击通知时,我想在 javascript 中调用一个函数。这就是我的需要。

标签: android push-notification android-pendingintent


【解决方案1】:

要显示吐司,请使用下面的行。

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

代替,

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

【讨论】:

  • 试过了..没有变化..还是一样
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多