Cannot get push notification from Urban Airship

喜欢而已 提交于 2019-12-05 01:59:16
TharakaNirmana

These are the steps you have to follow:

Android Side:

  • Create a sample app.
  • Include the jar of urban airship to your libs folder. I think latest jar is 3.0.
  • Create a file inside assets folder called: airshipconfig.properties.

These are what you have to put inside airshipconfig.properties:

// app ID of the project in google API's console, called 'Project Number' (in overview page)
gcmSender = 123456789
transport = gcm
developmentAppKey = mqqwqP1gLRkuf-bQBhlwOow
developmentAppSecret = 4kqqwqJfRGmSRBd40bA_Wg
inProduction = false

Android Manifest has to be as follows:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <!-- REQUIRED PERMISSIONS (for Urban Airship GCM) -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <!-- 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" />

    <!-- This app has permission to register with GCM and receive message -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.jbm.push.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.jbm.push.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:name="com.jbm.push.MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.jbm.push.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- REQUIRED for Urban Airship GCM -->
        <receiver android:name="com.urbanairship.CoreReceiver" />
        <receiver
            android:name="com.urbanairship.push.GCMPushReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <!-- MODIFICATION REQUIRED - Use your package name as the category -->
                <category android:name="com.jbm.push" />
            </intent-filter>
            <!-- REQUIRED for detecting when the application is upgraded so it can request a new GCM ID -->
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REPLACED" />

                <data android:scheme="package" />
            </intent-filter>
        </receiver>

        <service
            android:name="com.urbanairship.push.PushService"
            android:label="Push Notification Service" />
        <!-- REQUIRED only with UA Android lib version 2.1.4 and earlier. Do not include PushWorkerService with UA Android lib version 3.0.0+ since it has been deprecated -->
        <service
            android:name="com.urbanairship.push.PushWorkerService"
            android:label="Push Notification Worker Service" />
        <service
            android:name="com.urbanairship.analytics.EventService"
            android:label="Event Service" />

        <!-- This is required for persisting preferences related to push and location -->
        <!-- MODIFICATION REQUIRED - Use your package name + ".urbanairship.provider" in the android:authorities -->
        <provider
            android:name="com.urbanairship.UrbanAirshipProvider"
            android:authorities="com.jbm.push.urbanairship.provider"
            android:exported="false"
            android:multiprocess="true" />
        <!-- END OF REQUIRED ITEMS -->


        <!-- OPTIONAL (for segments support) -->
        <service
            android:name="com.urbanairship.location.LocationService"
            android:label="Segments Service" />

        <!-- OPTIONAL, if you want to receive push, push opened and registration completed intents -->
        <!-- Replace the receiver below with your package and class name -->
        <receiver android:name="com.jbm.push.IntentReceiver" />
    </application>

</manifest>

MyApplication.java:

public class MyApplication extends Application {

       @Override
       public void onCreate(){
           AirshipConfigOptions options = AirshipConfigOptions.loadDefaultOptions(this);
           UAirship.takeOff(this, options);
           PushManager.enablePush();

       }
    }

This is all that you require from the Android side. In order to send the push it is a must that you configure your app properly on urban airship site. Provide the android application's package name and the Google API Console project's server key: follow these steps: http://docs.urbanairship.com/dashboard/getting_started.html#configuring-services

Then simply send the message from the dashboard that airship has provided. I followed this tutorial:

http://docs.urbanairship.com/build/android.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!