简体   繁体   English

当我点击通知推送时,特定活动无法打开(需要真正的帮助)

[英]When i push notification clicked, specific activity doesn't open (need really help)

Not opening specific activity on notification click when the app is in background/not running 当应用程序在后台/未运行时,未在通知点击上打开特定活动

The notification-click starts specified activity only when the app is opened up and the notification-click is performed. 仅当打开应用程序并执行通知单击时,通知单击才会启动指定的活动。 If the app is in background/not running and the notification-click is performed, the application's MainActivity opens up. 如果应用程序在后台运行/未运行,并且执行了单击通知,则将打开应用程序的MainActivity。 In short, it is like the app opens normally following the activity stack instead of opening the specified activity in the PendingIntent. 简而言之,就像应用程序通常在活动堆栈之后打开,而不是在PendingIntent中打开指定的活动。

I saw a solution similar to this problem in this forum did not help unfortunately 不幸的是,我在该论坛上看到了与此问题类似的解决方案

MyApplication 我的应用程序

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        OneSignal.startInit(this).setNotificationOpenedHandler(new ExampleOpenHandler())
                .autoPromptLocation(true).init();

    }

    private class ExampleOpenHandler implements OneSignal.NotificationOpenedHandler {

        @Override
        public void notificationOpened(OSNotificationOpenResult result) {

            JSONObject data=result.notification.payload.additionalData;

            Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);

            try{

                intent.putExtra("datam",data.getString("1"));
            }catch (JSONException e) {
                e.printStackTrace();

            }
            startActivity(intent);
        }
    }
}

SecondActivity 第二活动

public class SecondActivity extends AppCompatActivity {

    TextView dataview;
    String getdata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        getdata = getIntent().getExtras().getString("datam");

        dataview = (TextView) findViewById(R.id.dataview);
        dataview.setText(getdata);
    }
}

Manifest 表现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="dede.com.dede">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <uses-feature
        android:name="android.hardware.camera"
        android:required="true" />

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Activity.girisKullaniciAdi"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Activity.TabbedActivity"
            android:screenOrientation="portrait"
            android:theme="@style/ThemeToolbar" />
        <activity
            android:name=".Activity.KameraActivity"
            android:parentActivityName=".Activity.AnaActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".Activity.MainActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".Activity.AnaActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.Anasayfa" />
        <activity
            android:name=".Activity.FizikselSayimKameraActivity"
            android:parentActivityName=".Activity.AnaActivity"
            android:screenOrientation="portrait"
            android:theme="@style/ThemeToolbar" />
        <activity
            android:name=".Activity.EkipmanGecmisiKameraActivity"
            android:parentActivityName=".Activity.AnaActivity"
            android:screenOrientation="portrait"
            android:theme="@style/ThemeToolbar" />
        <activity
            android:name=".Activity.EkipmanGecmisiActivity"
            android:screenOrientation="portrait"
            android:theme="@style/ThemeToolbar" />
        <activity
            android:name=".Activity.YeniIsEmriKameraActivity"
            android:parentActivityName=".Activity.AnaActivity"
            android:screenOrientation="portrait"
            android:theme="@style/ThemeToolbar" />

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />

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

        <activity android:name=".SecondActivity"></activity>

    </application>

</manifest>

activity_second.xml activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">

    <TextView
        android:id="@+id/dataview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="22sp"
        android:layout_margin="10dp"
        android:text="TextView" />

</android.support.constraint.ConstraintLayout>

To start an activity that includes a back stack of activities, you need to create an instance of TaskStackBuilder and call addNextIntentWithParentStack(), passing it the Intent for the activity you want to start. 要启动包含活动后退堆栈的活动,您需要创建TaskStackBuilder的实例并调用addNextIntentWithParentStack(),并向其传递您要启动的活动的Intent。

Follow this link : 点击此链接:

https://developer.android.com/training/notify-user/navigation https://developer.android.com/training/notify-user/navigation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM