草庐IT

android - OneSignal - 单击推送后无法打开 Activity

coder 2023-11-25 原文

我正在尝试。我从推送发送一些附加数据并将用户重定向到特定 Activity 但不重定向。

例如,我发送一个包含 AdditionalData(如 imageID)的推送,并通过将 imageID 参数传递给其他 Activity 来重定向用户 ImageDetail Activity 。

当我点击推送时,主 Activity 打开但没有任何反应

我试过了,但没有成功。

我该如何解决

public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

// nav drawer title
private CharSequence mDrawerTitle;

// used to store app title
private CharSequence mTitle;

// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;

private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
private static MainActivity mInstance;
private AccessToken facebookAccessToken;
private SessionManager session;
private String pushAdURL;
private boolean isAdActive =  false;
private boolean isQRActive = false;
private boolean isMaintenanceMode = false;
SharedPreferences sp ;
SharedPreferences.Editor editor;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sp = PreferenceManager.getDefaultSharedPreferences(this);

    Log.e("MAIN", "Main activity has been called");

    session = new SessionManager(getApplicationContext());

    OneSignal.startInit(this)
            .setNotificationOpenedHandler(new NotificationHandler())
            .init();





public static synchronized MainActivity getInstance() {
    return mInstance;
}




private class NotificationHandler implements OneSignal.NotificationOpenedHandler {
    /**
     * Callback to implement in your app to handle when a notification is opened from the Android status bar or
     * a new one comes in while the app is running.
     * This method is located in this Application class as an example, you may have any class you wish implement NotificationOpenedHandler and define this method.
     *
     * @param message        The message string the user seen/should see in the Android status bar.
     * @param additionalData The additionalData key value pair section you entered in on onesignal.com.
     * @param isActive       Was the app in the foreground when the notification was received.
     */
    @Override
    public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
        Toast.makeText(getApplicationContext(), "Notification opened:" + message
                + "Addional: "+additionalData, Toast.LENGTH_SHORT).show();

        Log.d("MESAJ:","message: " +message + "AditionData: " +String.valueOf(additionalData));

        try{

            if (additionalData != null) {
                Log.d("MESAJ:","Additionaldata is not null");

                if (additionalData.has("action")) {

                    Log.d("MESAJ:", "Title " + additionalData.getString("title"));
                    Log.d("MESAJ:", "Additionaldata has action");
                    Log.d("MESAJ:", "Action is " + additionalData.getString("action"));
                    Log.d("MESAJ:", "Action id is " + additionalData.getString("id"));


                    if (additionalData.getString("action") == "openimage") {
                        Log.d("MESAJ:","Additionaldata action is openimage");

                        String pusedImageId = additionalData.getString("id");
                        Log.d("MESAJ:", "Additionaldata action is " + pusedImageId);

                        Intent intent = new Intent(getApplicationContext(), ImageDetail.class);
                        intent.putExtra("imageid", pusedImageId);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    } else if (additionalData.getString("action") == "openboard") {
                        Log.d("MESAJ:","Additionaldata action is openboard");

                        String pusedBoardId = additionalData.getString("id");

                        Log.d("MESAJ:","Additionaldata action is " +pusedBoardId);

                        Intent intent = new Intent(getApplicationContext(), BoardDetail.class);
                        intent.putExtra("boardid", pusedBoardId);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);


                    }

                }
            }

        } catch (Throwable t) {
            t.printStackTrace();
        }

        /*try {
            if (additionalData != null) {
                if (additionalData.has("actionSelected"))
                    additionalMessage += "Pressed ButtonID: " + additionalData.getString("actionSelected");

                additionalMessage = message + "\nFull additionalData:\n" + additionalData.toString();
            }

            Log.d("OneSignalExample", "message:\n" + message + "\nadditionalMessage:\n" + additionalMessage);
        } catch (Throwable t) {
            t.printStackTrace();
        }
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Bilgilendirme");
        builder.setMessage(message + String.valueOf(additionalData));
        builder.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

            }
        });
        android.app.AlertDialog alert = builder.create();
        alert.show();*/
    }
}

这是 logcat 结果

    1-31 08:17:47.410 20682-20682/com.harmankaya.otokatalog D/MESAJ:: message: klAditionData: {"action":"openboard","id":"23123","title":"dsga"}
01-31 08:17:47.410 20682-20682/com.harmankaya.otokatalog D/MESAJ:: Additionaldata is not null
01-31 08:17:47.415 20682-20682/com.harmankaya.otokatalog D/MESAJ:: Title dsga
01-31 08:17:47.415 20682-20682/com.harmankaya.otokatalog D/MESAJ:: Additionaldata has action
01-31 08:17:47.415 20682-20682/com.harmankaya.otokatalog D/MESAJ:: Action is openboard
01-31 08:17:47.415 20682-20682/com.harmankaya.otokatalog D/MESAJ:: Action id is 23123
01-31 08:17:47.445 20682-20682/com.harmankaya.otokatalog D/OneSignal: curActivity is NOW: null
01-31 08:17:47.600 20682-20682/com.harmankaya.otokatalog V/BitmapFactory: DecodeImagePath(decodeResourceStream3) : res/drawable-xxhdpi-v4/sym_def_app_icon.png
01-31 08:17:47.610 20682-20682/com.harmankaya.otokatalog D/AbsListView: Get MotionRecognitionManager
01-31 08:17:47.615 20682-20682/com.harmankaya.otokatalog E/MAIN: Main activity has been called

编辑:嗯,我快要完成了 :) 我已经将这些行添加到 OnCreate 方法中,并且我想将 mu push redirect 逻辑移动到 MainActivity 的 OnCreate 方法中。但是现在我无法解析 Bundle Intent extras :)

        Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    Toast.makeText(getApplicationContext(), "Sonuç: " +bundle, Toast.LENGTH_SHORT).show();
    Log.d("mesaj", "Result: " + bundle);


    if (bundle != null) {
        try {
            //TODO Push redirect logic
            Log.d("mesaj","String bundle : "+bundle.getString("onesignal_data"));

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.d("PUSH",String.valueOf(e));
        }

    }

LOGCAT 结果

Sonuç: Bundle[{onesignal_data=[{"custom":"{\"a\":{\"action\":\"openboard\",\"id\":\"2345\"},\"i\":\"159c4c5d-37d2-45ec-ae90-6f103a4b8e83\"}","from":"111189706423","alert":"demopushbody","title":"demotitle","android.support.content.wakelockid":1,"collapse_key":"do_not_collapse"}]}]

最佳答案

我认为更好的方法是在您的 Activity 中添加以下行:

<application ...>
   <meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
</application>

关于android - OneSignal - 单击推送后无法打开 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35105612/

有关android - OneSignal - 单击推送后无法打开 Activity的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  3. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  4. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  5. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  6. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  7. ruby - 无法覆盖 irb 中的 to_s - 2

    我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)

  8. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

  9. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  10. ruby-on-rails - 无法让 rspec、spork 和调试器正常运行 - 2

    GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'

随机推荐