在UI上显示提醒

时间:2015-12-04 17:09:18

标签: android

我在LaunchActivity onCreate()

中运行以下内容
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            OneSignal.init(LaunchActivity.this, AppGlobal.GCMProjectNumber, AppGlobal.OneSignalAppID, new OneSignalNotificationOpened(LaunchActivity.this));
            Log.d("OneSignal", "Initialized");
            oneSignalInitialized = true;
        }
    });

这是我的OneSignalNotificationOpened课程:

class OneSignalNotificationOpened implements NotificationOpenedHandler {
    Context context;

    public OneSignalNotificationOpened(Context context) {
        this.context = context;
    }
    public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
        String messageTitle = "OneSignal Example", messageBody = message;
        String tag = "launch";

        try {
                if (additionalData.has("title")){
                    messageTitle = additionalData.getString("title");
                }
                if (additionalData.has("actionSelected")){
                    messageBody += "\nPressed ButtonID: " + additionalData.getString("actionSelected");
                }


                messageBody = message;
            if(additionalData.has("bookId")){
                bookIndex = Integer.parseInt(additionalData.getString("bookId"));
                if(bookIndex >= 0){
                    restrictBookListActivity = true;

                    if(AppPreferences.ShopBookList != null) {
                        Book theBook = AppPreferences.ShopBookList.get(bookIndex);
                        Log.i(tag, "stored: " + theBook.getName());
                        Bundle data = new Bundle();
                        data.putSerializable("param1", theBook);

                        //notificationReceived(messageTitle, messageBody, bookIndex);
                        final Intent detailActivity = new Intent(LaunchActivity.this, DetailActivity.class);
                        detailActivity.putExtras(data);
//                        startActivity(detailActivity);

                        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
                        dialog.setTitle("Notification received!");
                        dialog.setMessage("This is body");
                        dialog.setCancelable(true);
                        dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                startActivity(detailActivity);
                            }
                        });
                        dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        dialog.show();


                        //startActivity(detailActivity);
                    }else Log.i(tag, "book list is null");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            Log.d("Error", "Error: " + e.toString());
        }

    }
}

我使用Handler().postDelayed()中的onCreate()打开另一项活动,转到另一项活动。当我收到通知时,我参与了该活动。 dialog.show() OneSignalNotificationOpened中的WindowManager$BadTokenException: Unable to add window投掷isFinishing()。我该如何解决这个问题?

更新:我检查了我的活动是否完成,然后{{1}}显示对话框,发现它已完成。那么我该如何显示警报?

1 个答案:

答案 0 :(得分:0)

如果您只是在用户使用应用时收到通知时需要显示包含消息内容的对话框,则只需在OneSignal.enableInAppAlertNotification(true);后拨打OneSignal.init(...);即可。或者,如果您始终希望通知转到通知区域,则可以改为呼叫OneSignal.enableNotificationsWhenActive(true);

有关上述方法的详情,请参阅OneSignal Android API Reference页面。

如果您需要更自定义的警报对话框,则可以向每个Activity类扩展的BaseActivity类添加一个静态Activity变量。在BaseActivity的{​​{3}}方法中设置此静态变量,并将其用作new AlertDialog.Builder(context);的上下文