Android Push notification

## Firebase Console

https://console.firebase.google.com/?hl=ru&pli=1

## Ссылки

1. Push уведомления в Android. Грабли, костыли и велосипеды
2. Google cloud messaging Documentation
3. Google Cloud Messaging: «Сова, открывай! Пуш пришел!» Разработка под Android\*, Разраб

## FCM client setup

Libraries don't have an application ID. Application ID is supplied by the consumer of your library. Just include the dependency and that's it. Don't apply the google services plugin on a library.

Your client would have presumably followed https://firebase.google.com/docs/cloud-messaging/android/client tutorial. Which means you have to create your library's push message processor to which your client will delegate work.

As you can see connecting Firebase to your library is handled entirely by your library's consumer. You don't touch Firebase on client side at all.

## FCM server setup

You need an application ID to setup FCM. One application ID can only be associated to one Firebase Project, which would be that of your library's consumer. Dead end.

If you're running an online service and need to send push messages on behalf of your client you need them to create a Server key (restricted to your server's IP address) in Google Dev console and upload it along with Project ID to your server. These would allow your server to send push messages. This is essentially your client giving you permission to send push mesages on their behalf.

Mind potential security risks and abuse of your service.

## Creating Firebase library project

1. can create a firebase project for your library.

2. do-not add the google-service.json to the library.. each app should add its own

3. you can still create a token for the sender-id of your library-firebase-project using the API: firebaseInstanceId.getToken("sender-id-2", "FCM")

4. for the client setup the instructions above are correct. –

### Push Notification Types

// There are two types of messages data messages and notification messages. **Data messages** are handled

// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type

// traditionally used with GCM. **Notification messages** are only received here in onMessageReceived when the app

// is in the foreground. When the app is in the background an automatically generated notification is displayed.

// When the user taps on the notification they are returned to the app. Messages containing both notification

// and data payloads are treated as notification messages. The Firebase console always sends notification

// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options

## Перехват различного пэйлоада

// Check if message contains a data payload.

```
       if (remoteMessage.getData().size() > 0) {

```

```
           Log.d(TAG, "Message data payload: " + remoteMessage.getData());

```

```
       }

```

```
       // Check if message contains a notification payload.

```

```
       if (remoteMessage.getNotification() != null) {

```

```
           Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

```

```
       }

```

## Сборка приложения с пуш-уведомлениями

```
﻿Для сборки с помощью Gradle. Для сборки другими способами пропустить п. 1-3 и добавить либу google play services в приложение вручную.

1) В build.gradle уровня проекта добавить
    buildscript {
        dependencies {
            classpath 'com.google.gms:google-services:3.0.0'
        }
     }

 2) В build.gradle уровня модуля добавить
     dependencies {
        compile 'com.google.android.gms:play-services-analytics:9.6.1'
        compile 'com.google.android.gms:play-services-gcm:9.6.1'
    }
     apply plugin: 'com.google.gms.google-services'

3) В папку модуля нужно положить скачанный из google firebase console файл google-services.json

        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
        <permission
        	android:name="com.nevosoft.tsa.permission.C2D_MESSAGE""            android:protectionLevel="signature" />
         <uses-permission
            android:name="com.nevosoft.tsa.permission.C2D_MESSAGE" />
    4.2) Ресиверы и сервисы
        <!-- [START gcm_receiver] -->
         <receiver
             android:name="com.google.android.gms.gcm.GcmReceiver""            android:exported="true"e            android:permission="com.google.android.c2dm.permission.SEND" >
             <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.nevosoft.tsa" />
            </intent-filter>
        </receiver>
        <!-- [END gcm_receiver] -->
        <!-- [START gcm_listener] -->
         <service
             android:name="com.nevosoft.gcm.NsGcmListenerService""            android:exported="false" >
             <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <!-- [END gcm_listener] -->
         <!-- [START instanceId_listener] -->
         <service
             android:name="com.nevosoft.gcm.NsInstanceIDListenerService""            android:exported="false">
             <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID"/>
            </intent-filter>
        </service>
        <!-- [END instanceId_listener] -->
         <service
             android:name="com.nevosoft.gcm.RegistrationIntentService""            android:exported="false">
         </service>

5) Добавить содержимое папки res в res приложения

6) Положить файлы из java\com\nevosoft\gcm\ по соответствующему пути в приложение

7) Добавить код из java\com\nevosoft\main\AppActivity.java в тело главной Activity приложения

8) Проверить FIXME в коде из пп. 6-7. (Заменить имя класса главной Activity, добавить обработку отсыла regId на сервер и обработку пейлоада)

```