Push Notifications - Onesignal - Android - Tutorial

Discussion in 'How Can I...?' started by blad300, Oct 19, 2016.

  1. blad300

    blad300 Serious Boxer

    Joined:
    Oct 8, 2015
    Messages:
    515
    Likes Received:
    276
    Don't ask about IOS . i don't know .
    1. Create account here OneSignal and the Docs .
    2. Add new App .
    3. Go to developers.google.com and create a Google App. Read Docs
    4. App Settings->Platform->Android Google->Configure
    [​IMG]
    5. Add this settings
    [​IMG]
    Here you should have a error,it's say you don't have any subscriber. Press Yes .
    6. Implement (grandle or eclipse) OneSignal and Upgrading to Android SDK 3.0 (grandle or eclipse)
    Eclipse
    6.1 -> Download
    6.2 -> Copy OneSignalSDK.jar and android-support-v4.jar to libs .
    [​IMG]
    6.3 -> modify AndroidManifest.xml
    6.3.1 -> Add this code to <application ...>
    Code:
    <meta-data android:name="com.google.android.gms.version"
                 android:value="@integer/google_play_services_version" />
      <meta-data android:name="onesignal_app_id"
                 android:value="${onesignal_app_id}" />
      <meta-data android:name="onesignal_google_project_number"
                 android:value="str:${onesignal_google_project_number}" />
    
      <receiver android:name="com.onesignal.GcmBroadcastReceiver"
                android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
          <action android:name="com.google.android.c2dm.intent.RECEIVE" />
          <category android:name="${manifestApplicationId}" />
        </intent-filter>
      </receiver>
      <receiver android:name="com.onesignal.NotificationOpenedReceiver" />
      <service android:name="com.onesignal.GcmIntentService" />
      <service android:name="com.onesignal.SyncService" android:stopWithTask="false" />
      <activity android:name="com.onesignal.PermissionsActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    
      <service android:name="com.onesignal.NotificationRestoreService" />
      <receiver android:name="com.onesignal.BootUpReceiver">
        <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED" />
          <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
      </receiver>
      <receiver android:name="com.onesignal.UpgradeReceiver" >
        <intent-filter>
          <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
        </intent-filter>
      </receiver>
    6.3.2 -> Add this code to AndroidManifest.xml after user permisions.
    Code:
    <permission android:name="${manifestApplicationId}.permission.C2D_MESSAGE"
                android:protectionLevel="signature" />
    <uses-permission android:name="${manifestApplicationId}.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    <!-- START: ShortcutBadger -->
    <!-- Samsung -->
    <uses-permission android:name="com.sec.android.provider.badge.permission.READ"/>
    <uses-permission android:name="com.sec.android.provider.badge.permission.WRITE"/>
    
    <!-- HTC -->
    <uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS"/>
    <uses-permission android:name="com.htc.launcher.permission.UPDATE_SHORTCUT"/>
    
    <!-- Sony -->
    <uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE"/>
    <uses-permission android:name="com.sonymobile.home.permission.PROVIDER_INSERT_BADGE"/>
    
    <!-- Apex -->
    <uses-permission android:name="com.anddoes.launcher.permission.UPDATE_COUNT"/>
    
    <!-- Solid -->
    <uses-permission android:name="com.majeur.launcher.permission.UPDATE_BADGE"/>
    
    <!-- Huawei -->
    <uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE" />
    <uses-permission android:name="com.huawei.android.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.huawei.android.launcher.permission.WRITE_SETTINGS" />
    <!-- End: ShortcutBadger -->
    6.3.3 -> Replace all 3 of instances ${manifestApplicationId} with your package name(com.example.game) in AndroidManifest.xml.
    6.3.4 -> Replace ${onesignal_app_id} with your OneSignal app id.
    [​IMG]
    6.3.5 ->Replace ${onesignal_google_project_number} with your Google Project number. Note: str: is required. Example:
    <meta-data android:name="onesignal_google_project_number" android:value="str:123456789" />
    6.3.6 -> Save AndroidManifest.xml
    6.4 -> Add this text to Proguard.txt
    Code:
    -dontwarn com.onesignal.**
    
    -keep class com.google.android.gms.common.api.GoogleApiClient {
        void connect();
        void disconnect();
    }
    
    -keep public interface android.app.OnActivityPausedListener {*;}
    
    -keep class com.onesignal.shortcutbadger.impl.AdwHomeBadger { <init>(...); }
    -keep class com.onesignal.shortcutbadger.impl.ApexHomeBadger { <init>(...); }
    -keep class com.onesignal.shortcutbadger.impl.AsusHomeLauncher { <init>(...); }
    -keep class com.onesignal.shortcutbadger.impl.DefaultBadger { <init>(...); }
    -keep class com.onesignal.shortcutbadger.impl.HuaweiHomeBadger { <init>(...); }
    -keep class com.onesignal.shortcutbadger.impl.NewHtcHomeBadger { <init>(...); }
    -keep class com.onesignal.shortcutbadger.impl.NovaHomeBadger { <init>(...); }
    -keep class com.onesignal.shortcutbadger.impl.SolidHomeBadger { <init>(...); }
    -keep class com.onesignal.shortcutbadger.impl.SonyHomeBadger { <init>(...); }
    -keep class com.onesignal.shortcutbadger.impl.XiaomiHomeBadger { <init>(...); }
    6.4.1 -> Save Proguard.txt
    6.5 -> Open PTPlayer.java from "src/com.example.game/PTPlayer.java
    6.5.1 at line 13 add
    Code:
    import com.onesignal.OneSignal;
    [​IMG]
    6.5.2 at line 50 add
    Code:
    OneSignal.startInit(this).init();
    [​IMG]
    P.S -> every time i clean my project from Project->Clean...
    7 -> Export it and update to Magazin Play or install to your phone
    7.1 -> After your app is installed and you are open it, you are the first subscriber .
    7.2 -> Go back to OneSignal App Settings and open Android Google->Configure and save(you should have your first subscriber, YOU)
    [​IMG]
    Voila! you have now Push Notifications to your Game .
    Let's create first notification, you can do notification for a lot of stuff :
    Promote other games (with image 2:1 size and link directly to market store)
    If a player did't play for 48 hours, you can automate send him a notification to start your game and play .
    and many , many other things .
    Let's start with what i love it :
    Promote other games (with image 2:1 size and link directly to market store)
    Also here you can choose, when to do it :
    When the user returns to the app.
    When the user have andoid device (this is what i use)
    1 Create a new Segment -> New Segment -> Add Segment Name
    [​IMG]
    2 then go your Segment -> Add filter
    [​IMG]
    3 Create a new Template -> New Template
    3.1 Fill it with your details
    [​IMG]
    3.2 You can add details for other languages
    [​IMG]
    3.3 Add icons and big image
    [​IMG]
    3.4 Add url , when user click on notofication to go to market .
    [​IMG]
    and save it.
    4 Go To Automated -> New Automatic Message
    [​IMG]
    the other settings you can use them , depend of your push notification usage .
    and Create...

    You can wait the message or you can give a test .
    Test
    Go to your Templates -> choose option to your template and then "New message" .
    Click Preview - Send .
    Voila ! It's Romanian , because i send message in 20 different languages and i'm Romanian.

    [​IMG]
    [​IMG]

    Download My App and see how i send Notification . You can review also
     
    Last edited: Oct 19, 2016
  2. ramiucef

    ramiucef Avid Boxer

    Joined:
    Oct 3, 2015
    Messages:
    373
    Likes Received:
    177
    Great service and an amazing tutorial,
    thanks , i will definitly add it to my games ;)
     
    wgoodpaster and blad300 like this.
  3. Christoph

    Christoph Miniboss Boxer

    Joined:
    Oct 4, 2015
    Messages:
    2,807
    Likes Received:
    2,309
    This is amazing. Thanks for taking the time and sharing your knowledge.

    How does it impact performance if any? How many permissions appear additionaly when installing? (Best case would be one: do you allow the game to send you notifications?) and last but not least: is the service free even if you send it to millions of devices?
     
    wgoodpaster and SuperVudu like this.
  4. blad300

    blad300 Serious Boxer

    Joined:
    Oct 8, 2015
    Messages:
    515
    Likes Received:
    276
    I use before their services and never pay a cent .
    I use it for website its first time when i use for games.
    Every major app have this push notifications, you don't see them ? Google Mail, Instagram, and every app or game with push notifications.

    In OneSignal you have a dashboard with :
    How many message sent, how many opened, how many users subscribed to your game.
    and any impact you have.

    If you send 20 messages per day, maybe user can unnistal your game, but i think if you have good segment,like people they didn't play in last 6-7 days, you send them a message to remeber them is there a game and they must play it :)) .

    Or you release another game and you want to promote it, why do not use your own subscribers ? you sent them an notification with your fabulos game .

    Or you want to ask them about a review , send them an notification with review link .
    (for that you select any user that play more than 20-30 sessions, and automate a message with review link) too many way to use it :)

    You must interact with your "customers" , and you must interact with them nice and shine :))

    PS - IF YOU HAVE MILLIONS MESAGE TO SEND, IT'S MEAN YOU HAVE ALSO MONEY, SO YOU CAN PAY 1%-2% FROM YOUR PROFIT. @Christoph
     
    Last edited: Oct 19, 2016
    wgoodpaster and Christoph like this.
  5. jcalle

    jcalle Miniboss Boxer

    Joined:
    Sep 25, 2015
    Messages:
    1,192
    Likes Received:
    540
    Thanks for the tutorial.
     
    blad300 likes this.
  6. Arsmen Studio

    Arsmen Studio Avid Boxer

    Joined:
    Dec 6, 2015
    Messages:
    454
    Likes Received:
    170
    errors
    -Description Resource Path Location Type
    app_id cannot be resolved or is not a field PTPlayer.java /com.arsmen.superdariosworld/src/com/arsmen/superdariosworld line 53 Java Problem

    -Description Resource Path Location Type
    Tag <category> attribute name has invalid character '$'. AndroidManifest.xml /com.arsmen.superdariosworld line 88 Android AAPT Problem

    -Description Resource Path Location Type
    Tag <permission> attribute name has invalid character '$'. AndroidManifest.xml /com.arsmen.superdariosworld line 122 Android AAPT Problem

    -Description Resource Path Location Type
    Tag <uses-permission> attribute name has invalid character '$'. AndroidManifest.xml /com.arsmen.superdariosworld line 124 Android AAPT Problem


    -Description Resource Path Location Type
    The import com.google.android.gms.R cannot be resolved PTServicesBridge.java /com.arsmen.superdariosworld/src/com/secrethq/utils line 14 Java Problem
     
    Last edited: Dec 23, 2016
    wgoodpaster likes this.
  7. gheorghe9512

    gheorghe9512 Boxer

    Joined:
    Dec 20, 2015
    Messages:
    70
    Likes Received:
    46
  8. blad300

    blad300 Serious Boxer

    Joined:
    Oct 8, 2015
    Messages:
    515
    Likes Received:
    276
    pai instaleaza jocu pe telefou tau sa fi primu tau subscriber
     
  9. redmondking

    redmondking Boxer

    Joined:
    Oct 8, 2016
    Messages:
    5
    Likes Received:
    0
    Hey @blad3000 can you please update this tutorial? There have been some changes and was wondering if you could update this. Would help greatly.

    or if you could inform us which version of the one signal sdk you are using.

    Thanks.
     
    Last edited: Jan 28, 2017
  10. gheorghe9512

    gheorghe9512 Boxer

    Joined:
    Dec 20, 2015
    Messages:
    70
    Likes Received:
    46
    nu imi apare nimic in telefon
     
  11. blad300

    blad300 Serious Boxer

    Joined:
    Oct 8, 2015
    Messages:
    515
    Likes Received:
    276
    I know they made some changes,but not so critical to upgrade this tutorial.
    I always download latest sdk from onesignal.com website.
     
    Christoph likes this.
  12. blad300

    blad300 Serious Boxer

    Joined:
    Oct 8, 2015
    Messages:
    515
    Likes Received:
    276
    Ce telefon ai ? este foarte usor sa implementezi sdk de la onesignal.
     
  13. gheorghe9512

    gheorghe9512 Boxer

    Joined:
    Dec 20, 2015
    Messages:
    70
    Likes Received:
    46
    Samsung Galaxy A5
     
  14. Appymes

    Appymes Avid Boxer

    Joined:
    Nov 11, 2016
    Messages:
    268
    Likes Received:
    59
    This error in Eclipse?

    [​IMG]

    Dex Loader] Unable to execute dex: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;

    Thanks!
     
  15. Appymes

    Appymes Avid Boxer

    Joined:
    Nov 11, 2016
    Messages:
    268
    Likes Received:
    59
    Christoph and blad300 like this.
  16. Christoph

    Christoph Miniboss Boxer

    Joined:
    Oct 4, 2015
    Messages:
    2,807
    Likes Received:
    2,309
    I had the same problem as @Appymes. The reason for the Dex error is the duplicated files. in my case:
    - android-support-v4.jar (which is needed by OneSignal)
    - support-v4.-19.0.1.jar (which is already part of muy build)

    Solution:
    Just delete the one that is already part of your project and use the one that comes with OneSignal (android-support-v4.jar). This makes it work.

    Also, there is one change in code. The part where you need to replace the onesignal google project number (the part with the string). It simply isn't necessary anymore.
    Code:
        <meta-data android:name="onesignal_google_project_number"
                 android:value="str:${onesignal_google_project_number}" />
    It works either way though, including it or not.
    Source: https://documentation.onesignal.com/docs/android-sdk-setup#section-2-androidmanifest-xml see point 2.2. As you can see this part of the code is missing now. :)

    Thanks Appymes for pointing out the solution, it was driving me nuts. And thank you very much for the complete tutorial @blad300. Awesome share and I finally had time to check it out. Works with the latest version 3.4.3!
     
  17. blad300

    blad300 Serious Boxer

    Joined:
    Oct 8, 2015
    Messages:
    515
    Likes Received:
    276
    I never keep support-v4.-19.0.1.jar from buildbox, i use from onesignal all .

    Yes they update it, short time after i made this tutorial .
    they reove that part of code.
     
    Christoph likes this.
  18. ZyzygyGames

    ZyzygyGames Avid Boxer

    Joined:
    Dec 8, 2016
    Messages:
    123
    Likes Received:
    13
    What is this?
    <meta-data android:name="onesignal_google_project_number" android:value="str:123456789" />

    What is that onesignal_google_project_number?
     
  19. blad300

    blad300 Serious Boxer

    Joined:
    Oct 8, 2015
    Messages:
    515
    Likes Received:
    276
  20. ZyzygyGames

    ZyzygyGames Avid Boxer

    Joined:
    Dec 8, 2016
    Messages:
    123
    Likes Received:
    13
    Is it the ID?
     

Share This Page