【问题标题】:Get local notification while app is forced closed in android在android中强制关闭应用程序时获取本地通知
【发布时间】:2014-08-17 00:25:00
【问题描述】:

在 Facebook android 应用程序中,当应用程序被强制关闭时,我们会收到通知。它之所以有效,是因为实现了推送通知。但是我已经在我的应用程序中实现了本地通知,如果应用程序被强制关闭,我希望我的应用程序通知。那么我该如何实现呢。

        void showNotify()
        {
            CharSequence details = "Notification raised";
    int reqCode=0;
    Intent notificationIntent = new Intent(context,
            NotifyListActivity.class);
    // notificationIntent.putExtra("clicked", "Notification Clicked");
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_SINGLE_TOP); // To open only one   activity
                                                // on launch.
    PendingIntent pIntent = PendingIntent.getActivity(context, reqCode,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager nM = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notify = new NotificationCompat.Builder(
            context);
    notify.setContentIntent(pIntent);
    notify.setSmallIcon(R.drawable.appicon);
    notify.setContentTitle("");
    notify.setContentText(details);
    notify.setAutoCancel(true);
    nM.notify(reqCode, notify.build());
    screenOn(context);
            }

【问题讨论】:

    标签: android notifications android-notifications android-notification-bar


    【解决方案1】:

    您可以将其用于异常处理:Android exception handling best practice? 有了它,您可以在引发未处理的异常时执行代码。你也可以抓住它们,但这对你来说可能不是有趣的事情。

    然后在 UncaughtExceptionHandler 内部,我将启动一个前台服务。这是必要的,因为应用程序将关闭。我没测试,告诉我它是否有效。这将是服务的 onCreate:

        @Override public void onCreate() {
           final NotificationCompat.Builder builder = new Builder(this);
           builder.setSmallIcon(R.drawable.icon_resource);
           builder.setContentTitle(getString(R.string.notification_title));
           builder.setTicker(getString(R.string.notification_ticker));
           builder.setContentText(getString(R.string.notification_content_text));
    
           final Intent notificationIntent = new Intent(this, Activity_TO_OPEN_ON_NOTIFICATION_CLICKED.class);
    
           final PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    
           builder.setContentIntent(pi);
    
           final Notification notification = builder.build();
    
           startForeground(1234, notification);
           }
    

    【讨论】:

      【解决方案2】:

      我认为你不应该控制你的应用程序是否被强制关闭,如果你的应用程序被强制关闭,IMO 行为不正常是正常的。

      话虽如此,这两件事完全不同,当您收到推送通知时,您会收到来自 Internet(或其他来源)的消息,然后您可以回复该消息,显示本地通知(在您的通知栏中),您将需要一种方法来触发本地通知以便显示(重复警报?),通知不能自己启动(或者至少我不知道该怎么做)。

      【讨论】:

      • 不,我使用了重复警报但在强制关闭后它不起作用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多