OneSignal - 推送点击后无法打开活动

时间:2016-01-30 18:43:48

标签: android android-intent push-notification push onesignal

我正在尝试。我从push发送一些AdditionalData并将用户重定向到特定活动但不重定向。

例如,我发送推送包含像dataID这样的AdditionalData,并将用户ImageDetail活动重定向到其他活动。

当我点击推送时,主要活动打开,没有任何反应

我试过但不能成功。

我该如何解决?

49

}

这是logcat结果

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();*/
    }
}

EDİT:我即将完成:)我已经将这些行添加到OnCreate方法中,我想将mu push重定向逻辑移动到MainActivity的OnCreate方法。但现在我无法解析Bundle Intent临时演员:)

    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

LOGCAT结果

        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));
        }

    }

3 个答案:

答案 0 :(得分:9)

我认为更好的方法是在你的活动中添加以下行:

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

答案 1 :(得分:6)

默认情况下,OneSignal会在打开通知时打开启动器活动。在您的情况下,您的ImageDetail活动可能正在启动,然后启动启动器活动。

您需要将com.onesignal.NotificationOpened.RECEIVE添加到BroadcastReceiver以覆盖打开启动器Activity的默认操作。有关所需的清单条目和更多详细信息,请参阅One {s}在Changing the open action of a notification上的文档。您的BroadcastReceiver不需要任何代码来执行任何操作,因为您已在Application类中设置了OneSignal.NotificationOpenedHandler。清单和接收器只需要存在以覆盖默认行为。

答案 2 :(得分:2)

终于是的。我得到推送信息和重定向。

       Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    Log.d("mesaj", "Sonuç: " + bundle);


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

            String jsonData = bundle.getString("onesignal_data");
            Log.d("mesaj", "json: " + jsonData);

            if (jsonData != null) {
                JSONArray json = new JSONArray(jsonData);

                Log.d("mesaj", "json size: " + json.length());

                if (json.length() < 2) {

                    Log.d("mesaj", " siz < 2 json get0: " + json.get(0));

                } else {

                    for (int i = 1; i < json.length(); i++) {
                        object = json.getJSONObject(i);
                        Log.d("mesaj", "json details: " + object.getString("custom") + object.getString("alert"));
                        String obj = new JSONObject(object.getString("custom")).getString("a");
                        String action = new JSONObject(obj).getString("action");
                        String actionid = new JSONObject(obj).getString("id");

                        if (action.equals("openboard")) {
                            Intent intent2 = new Intent(MainActivity.this, BoardDetail.class);
                            intent2.putExtra("boardid", actionid);
                            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent2);
                        } else if (action.equals("openimage")) {
                            Intent intent2 = new Intent(MainActivity.this, ImageDetail.class);
                            intent2.putExtra("imageid", actionid);
                            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent2);
                        } else if (action.equals("openblog")) {
                            Intent intent2 = new Intent(MainActivity.this, BlogDetail.class);
                            intent2.putExtra("blogid", actionid);
                            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent2);
                        }
                    }

                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
            Log.d("Push Exception : ", String.valueOf(e));
        }

    }