【问题标题】:Open android app from PUSH notification从 PUSH 通知中打开 android 应用
【发布时间】:2012-04-28 23:57:50
【问题描述】:

有个小问题一直困扰着我..

我已将我的应用程序设置为接收来自 Urban Airship 的 PUSH 通知,并且一切正常,但是当我在通知中心点击通知时,什么也没有发生。

我希望我的应用在用户点击推送通知时打开 - 我可以做些什么来实现这一点?

我们一如既往地非常感谢任何帮助。

谢谢

【问题讨论】:

  • 你的申请进程是否在后台运行?

标签: android push-notification android-pendingintent urbanairship.com


【解决方案1】:

创建一个挂起的 Intent 来启动 Activity 并使用 setLatestEventInfo 在通知中设置它。

例子:

  Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

更多信息可以找到here

【讨论】:

  • 这段代码去哪儿了?我和最初的海报有同样的问题,但我没有找到合乎逻辑的地方。如果重要的话,我会使用 Pushwoosh 发送通知。
  • 此代码用于创建通知。你有任何接收器类...?将其放在 onreceive 中。
【解决方案2】:

您需要使用自定义通知构建器并将您的一项活动用作 PendingIntent。

https://docs.urbanairship.com/android-lib/reference/com/urbanairship/push/CustomPushNotificationBuilder.html

【讨论】:

    【解决方案3】:

    按照他们的示例项目之一 (https://github.com/urbanairship/android-samples/tree/master/app/src/main/java/com/urbanairship/sample),您可以扩展 AirshipReceiver 类,然后覆盖 onReceive 方法。这对我有用:

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    
        String action = intent.getAction();
        if (action.equals(PushManager.ACTION_NOTIFICATION_OPENED)) {
            Intent launch = new Intent(Intent.ACTION_MAIN);
            launch.setClass(UAirship.shared().getApplicationContext(), MyHome.class);
            launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            launch.putExtra("doWhatever",true);
            UAirship.shared().getApplicationContext().startActivity(launch);
        }
    
    }
    

    【讨论】:

      【解决方案4】:

      你必须在调用 .setAutoCancel(true); 之前调用 pendingIntent;

      看看我的建造者:

          NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),channel_id).setSmallIcon(R.drawable.logo).setSound(uri).setVibrate(new long[]{1000,1000,1000,1000,1000})
                  .setOnlyAlertOnce(true).setContentIntent(pendingIntent).setAutoCancel(true);
      

      【讨论】:

        猜你喜欢
        • 2021-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-11
        • 1970-01-01
        • 1970-01-01
        • 2014-10-29
        • 2023-03-11
        相关资源
        最近更新 更多