简体   繁体   中英

pushwoosh onMessageReceive xamarin android

ok, hello i am trying to implement pushwoosh notification on my xamarin.android application

i am on a stage were notification sent to client and when i click the notification it should redirect me on a certain activity and ui, here is my code

private void DoOnMessageReceive(String message)
{
var messageJson = new JSONObject(message);
if (messageJson.GetString("title") == "akotube")
{
    var intent = new Intent(this, typeof(Second));
    //intent.PutExtra(PushManager.PushReceiveEvent, messageJson.ToString());
    intent.PutExtra("message", "akotube");
    StartActivity(intent);
}

` but i cant make it work , this is my reference pushwoosh


EDIT Here is my manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest android:versionCode="1" android:versionName="1.0" package="com.pushwosh.sample" xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
    <!--library-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <!--
    Creates a custom permission so only this app can receive its messages.
    NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
    where PACKAGE is the application's package name.
    -->
    <permission android:name="com.pushwosh.sample.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
    <uses-permission android:name="com.pushwosh.sample.permission.C2D_MESSAGE"/>
    <!-- This app has permission to register and receive data message. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
    <!-- GCM requires Android SDK version 2.2 (API level 8) or above. -->
    <!-- The targetSdkVersion is optional, but it's always a good practice
    to target higher versions. -->
    <application android:icon="@drawable/Icon" android:label="PushWosh">
        <meta-data android:name="PW_APPID" android:value="CXXXX-XXXX"/>
        <meta-data android:name="PW_PROJECT_ID" android:value="AXXXXXXXXX"/>
        <activity android:name="com.arellomobile.android.push.PushWebview"/>
        <activity android:name="com.arellomobile.android.push.MessageActivity"/>
        <activity android:name="com.arellomobile.android.push.PushHandlerActivity"/>
        <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
                <!-- Receives the registration id. -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
                <category android:name="com.pushwosh.sample"/>
            </intent-filter>
        </receiver>
        <service android:name="com.arellomobile.android.push.PushGCMIntentService"/>
    </application>
</manifest>

here is my class

protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        var btn = FindViewById<Button>(Resource.Id.MyButton);
        btn.Click += btn_Click;
        var goSecond = FindViewById<Button>(Resource.Id.btnGo);
        goSecond.Click += goSecond_Click;
        var unregister = FindViewById<Button>(Resource.Id.Unregister);
        unregister.Click += unregister_Click;

        _mMessageReceiver = new LocalMessageBroadcastReceiver {Activity = this};

        _mRegisterReceiver = new LocalRegisterBroadcastReceiver {Activity = this};

        _manager = PushManager.GetInstance(this);


        //Register for push!
        //_manager.RegisterForPushNotifications();

        CheckMessage(Intent);

    }

 private void RegisterReceivers()
    {
        var intentFilter = new IntentFilter(PackageName + ".action.PUSH_MESSAGE_RECEIVE");

        if (MBroadcastPush)
        {
            RegisterReceiver(_mMessageReceiver, intentFilter);
        }

        RegisterReceiver(_mRegisterReceiver, new IntentFilter(PackageName + "." + PushManager.RegisterBroadCastAction));
    }

    private void UnregisterReceivers()
    {
        UnregisterReceiver(_mMessageReceiver);
        UnregisterReceiver(_mRegisterReceiver);
    }

    class LocalMessageBroadcastReceiver : BasePushMessageReceiver
    {
        public MainActivity Activity { private get; set; }

        protected override void OnMessageReceive(Intent intent)
        {
            Activity.DoOnMessageReceive(intent.GetStringExtra(JsonDataKey));

        }
    }

    class LocalRegisterBroadcastReceiver : RegisterBroadcastReceiver
    {
        public MainActivity Activity { private get; set; }

        protected override void OnRegisterActionReceive(Context p0, Intent intent)
        {
            Activity.CheckMessage(intent);
        }
    }

private void CheckMessage(Intent intent)
    {
        if (null != intent)
        {
            if (intent.HasExtra(PushManager.PushReceiveEvent))
            {
                DoOnMessageReceive(intent.Extras.GetString(PushManager.PushReceiveEvent));
            }
            else if (intent.HasExtra(PushManager.RegisterEvent))
            {
                DoOnRegistered(intent.Extras.GetString(PushManager.RegisterEvent));
            }
            else if (intent.HasExtra(PushManager.UnregisterEvent))
            {
                DoOnUnregisteredError(intent.Extras.GetString(PushManager.UnregisterEvent));
            }
            else if (intent.HasExtra(PushManager.RegisterErrorEvent))
            {
                DoOnRegisteredError(intent.Extras.GetString(PushManager.RegisterErrorEvent));
            }
            else if (intent.HasExtra(PushManager.UnregisterErrorEvent))
            {
                DoOnUnregistered(intent.Extras.GetString(PushManager.UnregisterErrorEvent));
            }

            ResetIntentValues();
        }
    }

Your Category in Activity needs to match your Activity's Intent filter

Also

Getting the intent and starting the activity on your DoOnMessageRecieved

Intent intent = new Intent(this, typeof(SecondActivity));
intent.PutExtra(PushManager.PushReceiveEvent, messageJson.ToString());
StartActivity(intent);

Where the SecondActivity is the activity you want to navigate to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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