单击推送通知时,特定活动不会打开

时间:2018-07-13 12:28:57

标签: android android-layout android-gradle

当应用程序在后台/未运行时,未在通知点击上打开特定活动

仅当打开应用程序并执行通知单击时,通知单击才会启动指定的活动。如果应用程序在后台运行/未运行,并且执行了单击通知,则将打开应用程序的MainActivity。简而言之,就像应用程序通常在活动堆栈之后打开,而不是在PendingIntent中打开指定的活动一样。

不幸的是,我在该论坛上看到了与此问题类似的解决方案

MyApplication

    import android.app.Application;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;

import org.json.JSONException;
import org.json.JSONObject;

public class MyApplication extends Application {

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

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

    }
    private void startNotification() { Log.i("NextActivity", "startNotification");

// Sets an ID for the notification int mNotificationId = 001;

// Build Notification , setOngoing keeps the notification always in status bar
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.bugununfaaliyet)
                        .setContentTitle("Stop LDB")
                        .setContentText("Click to stop LDB")
                        .setOngoing(true);

// Create pending intent, mention the Activity which needs to be
//triggered when user clicks on notification(StopScript.class in this case)

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, SecondActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);


        mBuilder.setContentIntent(contentIntent);


// Gets an instance of the NotificationManager service
        NotificationManager mNotifyMgr =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

// Builds the notification and issues it.
        mNotifyMgr.notify(1, mBuilder.build());
    }


    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

package blabla.com.blabla;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

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

AndroidManifest

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

    <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>

1 个答案:

答案 0 :(得分:0)

请查看此答案。

private void startNotification() {
    Log.i("NextActivity", "startNotification");

 // Sets an ID for the notification
      int mNotificationId = 001;

    // Build Notification , setOngoing keeps the notification always in status bar
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ldb)
                    .setContentTitle("Stop LDB")
                    .setContentText("Click to stop LDB")
                    .setOngoing(true);

    // Create pending intent, mention the Activity which needs to be 
    //triggered when user clicks on notification(StopScript.class in this case)

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, StopScript.class), PendingIntent.FLAG_UPDATE_CURRENT);


    mBuilder.setContentIntent(contentIntent);


    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Builds the notification and issues it.
    mNotifyMgr.notify(mNotificationId, mBuilder.build());


}

// StopScript是您的活动名称