解析推送通知 - 没有出现警告对话框时,应用程序被关闭 [英] Parse push notification - alert dialog not appearing when app is closed

查看:202
本文介绍了解析推送通知 - 没有出现警告对话框时,应用程序被关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来解析,并取得了接收推送通知的应用程序。我有我发送给它包含一个URL应用一个JSON有效载荷。当设备接收到推送通知,将会出现一个警告对话框,取消和确定按钮。如果用户presses确定,然后在网页加载。

这工作完全正常,当应用程序在设备上打开的,但是当应用程序无法在设备上打开失败。通知仍出现,用户可以点击它来打开应用程序时,它在后台运行,但会出现在屏幕上没有任何对话。如果应用程序是完全关闭,然后我看到一个弹出告诉我,该应用程序崩溃,但随后的应用程序将加载,但没有对话框会在屏幕上显示。这里是我的接收器是什么样子:

 公共类MyCustomReceiver扩展广播接收器{
  私有静态最后弦乐TAG =MyCustomReceiver;
  公共静态最后弦乐ACTION =custom_action_name;
  @覆盖
  公共无效的onReceive(上下文的背景下,意图意图){
    尝试{
      ...
      ...
      迭代器ITR = json.keys();
      而(itr.hasNext()){
        字符串键=(字符串)itr.next();
        ...
        如果(key.equals(targetUrl这个)){
            MainActivity.showdialog(json.getString(键));
        }
        ...
      }
      字符串消息= json.getString(警告);
    }赶上(JSONException E){
      Log.d(TAGJSONException:+ e.getMessage());
    }
  }
}

所以当迭代器击中 targetUrl这个键,ShowDialog的()方法被调用并通过在URL中。

下面是我ShowDialog方法,在我MainActivity类别找到:

 公共静态无效的ShowDialog(最终字符串URL){
    AlertDialog.Builder警报=新AlertDialog.Builder(背景);
    alert.setTitle(标题);
    alert.setMessage(信息);
    alert.setPositiveButton(OK,新DialogInterface.OnClickListener(){        @覆盖
        公共无效的onClick(DialogInterface对话,诠释它){
            URI URI = Uri.parse(URL);
            意向意图=新意图(Intent.ACTION_VIEW,URI);
            context.startActivity(意向);
        }
    });
    alert.setNegativeButton(取消,新DialogInterface.OnClickListener(){        @覆盖
        公共无效的onClick(DialogInterface对话,诠释它){        }
    });
    alert.show();
}

再次 - 当应用程序打开时能正常工作,但如果应用程序被关闭失败。我怎样才能让用户通过通知打开应用程序,看到这个对话框?

下面是错误堆栈如果应用程序被关闭,我试图通过推送通知开:

 致命异常:主要
过程:././,PID:30566
了java.lang.RuntimeException:无法启动接收././.MyCustomReceiver:显示java.lang.NullPointerException
在android.app.ActivityThread.handleReceiver(ActivityThread.java:2856)
在android.app.ActivityThread.access $ 1700(ActivityThread.java:156)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1440)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:157)
在android.app.ActivityThread.main(ActivityThread.java:5872)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:515)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:858)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
在dalvik.system.NativeStart.main(本机方法)
显示java.lang.NullPointerException:产生的原因
在android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
在android.app.AlertDialog $生成器<&初始化GT;(AlertDialog.java:360)
在././.MainActivity.showdialog(MainActivity.java:140)
在././.MyCustomReceiver.onReceive(MyCustomReceiver.java:34)
在android.app.ActivityThread.handleReceiver(ActivityThread.java:2845)


解决方案

我有同样的问题与解析通知。以下是我得到了我的工作。


  1. 找出当前的活动,请参阅 http://stackoverflow.com/a/13994622/2383911

  2. 如果当前活动== NULL,这意味着应用程序的后台运行

  3. 打开最后一个活动

  4. 等待2秒(为了安全起见)来传递的意图回的onReceive功能。

  5. 从那里您alertdialog功能应该工作。

    公共无效的onReceive(最终上下文的背景下,最终的意图意图){

    currentActivity =((启动)context.getApplicationContext())getCurrentActivity();

     如果(currentActivity == NULL){
            意图notificationIntent =新意图(背景下,Login.class);
            notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            notificationIntent.setAction(Intent.ACTION_MAIN);
            最后的处理程序H =新的处理程序();
            最终诠释延迟= 1000; //毫秒        h.postDelayed(新的Runnable(){
                公共无效的run(){
                    的onReceive(上下文,意图);            }
            },延迟);
        }


编辑:进一步测试这个真正的解决方案为哈克后,我发现,如果你有等待处理,这种方法会为每一个悬而未决的通知alertdialogs,并把它们放在彼此顶部...如果任何人都可以找出多个通知如何只能让选择通知显示出来,我会永远感激!更多信息这里 - <一个href=\"http://stackoverflow.com/questions/26020523/displaying-a-parse-com-push-notification-with-alert-and-title-keys-as-an-ale\">Displaying警报&QUOT与......一个Parse.com推送通知;和&QUOT;标题&QUOT;键时,应用程式在后台运行的 alertdialog

I'm new to Parse, and have made an app that receives push notifications. I have a json payload that I am sending to the app which contains a URL. When the push notification is received by the device, an alert dialog appears with "cancel" and "OK" buttons. If the user presses "OK", then the webpage is loaded.

This works perfectly fine when the application is open on the device, but fails when the application is not open on the device. The notification still appears and the user can click on it to open the application when it is in the background, but no dialog will appear on the screen. If the app is fully closed, then I see a popup telling me that the application has crashed, but then the application will load but no dialog box will be visible on the screen. Here's what my receiver looks like:

public class MyCustomReceiver extends BroadcastReceiver {
  private static final String TAG = "MyCustomReceiver";
  public static final String ACTION = "custom_action_name";
  @Override
  public void onReceive(Context context, Intent intent) {
    try {
      ...
      ...
      Iterator itr = json.keys();
      while (itr.hasNext()) {
        String key = (String) itr.next();
        ...
        if (key.equals("targetUrl")){
            MainActivity.showdialog(json.getString(key));
        }
        ...
      }
      String message = json.getString("alert");
    } catch (JSONException e) {
      Log.d(TAG, "JSONException: " + e.getMessage());
    }
  }
}

so when the iterator hits the targetUrl key, the showdialog() method is called and passes in the url.

Here is my showdialog method, found in my MainActivity class:

public static void showdialog(final String url){
    AlertDialog.Builder alert = new AlertDialog.Builder(context);
    alert.setTitle("Title");
    alert.setMessage("Message");
    alert.setPositiveButton("OK",new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW,uri);
            context.startActivity(intent);
        }
    });
    alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    alert.show();
}

Again - this works fine when the app is open, but fails if the app is closed. How can I have the user open up the app through the notification and see this dialog?

Here is the error stack if the application is closed and I try to open through the push notification:

FATAL EXCEPTION: main
Process: ././., PID: 30566
java.lang.RuntimeException: Unable to start receiver ././.MyCustomReceiver: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2856)
at android.app.ActivityThread.access$1700(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
at android.app.AlertDialog$Builder.<init>(AlertDialog.java:360)
at ././.MainActivity.showdialog(MainActivity.java:140)
at ././.MyCustomReceiver.onReceive(MyCustomReceiver.java:34)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2845)

解决方案

I had the same issue with Parse Notifications. Here's how I got mine to work.

  1. find out the current activity, see http://stackoverflow.com/a/13994622/2383911
  2. if current activity == null, that means app is backgrounded
  3. open the last activity
  4. wait 2 seconds (to be safe) to pass the intent back to the onReceive function.
  5. from there your alertdialog function should work.

    public void onReceive(final Context context, final Intent intent) {

    currentActivity = ((Launch) context.getApplicationContext()).getCurrentActivity();

        if (currentActivity == null) {
            Intent notificationIntent = new Intent(context, Login.class);
            notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            notificationIntent.setAction(Intent.ACTION_MAIN);
            final Handler h = new Handler();
            final int delay = 1000; //milliseconds
    
            h.postDelayed(new Runnable() {
                public void run() {
                    onReceive(context, intent);
    
                }
            }, delay);
        }
    

Edit: After further testing this real hacky solution- I found out that if you have multiple notifications pending, this method will create alertdialogs for every single pending notification and put them on top of each other... if anyone can figure out how to only make the chosen notification show up, I'd be forever grateful! more info here- Displaying a Parse.com push notification with "alert" and "title" keys as an alertdialog when app is backgrounded

这篇关于解析推送通知 - 没有出现警告对话框时,应用程序被关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆