{"id":4580,"url":"https://github.com/oney/react-native-gcm-android","last_synced_at":"2025-04-09T18:16:24.772Z","repository":{"id":57337182,"uuid":"46122643","full_name":"oney/react-native-gcm-android","owner":"oney","description":"GCM for React Native Android","archived":false,"fork":false,"pushed_at":"2016-08-12T17:00:24.000Z","size":55,"stargazers_count":172,"open_issues_count":38,"forks_count":75,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-09T18:16:19.682Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oney.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-13T13:10:30.000Z","updated_at":"2025-03-29T21:27:49.000Z","dependencies_parsed_at":"2022-09-12T13:13:00.387Z","dependency_job_id":null,"html_url":"https://github.com/oney/react-native-gcm-android","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oney%2Freact-native-gcm-android","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oney%2Freact-native-gcm-android/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oney%2Freact-native-gcm-android/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oney%2Freact-native-gcm-android/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oney","download_url":"https://codeload.github.com/oney/react-native-gcm-android/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248085322,"owners_count":21045139,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-01-05T20:17:16.958Z","updated_at":"2025-04-09T18:16:24.745Z","avatar_url":"https://github.com/oney.png","language":"Java","funding_links":[],"categories":["Components","\u003ca name=\"Network:-Native-Modules\"\u003eNetwork: Native Modules\u003c/a\u003e"],"sub_categories":["System"],"readme":"# react-native-gcm-android\n\nGCM for React Native Android\n\n## Demo\n\nhttps://github.com/oney/TestGcm\n\n## Installation\n\n- Run `npm install react-native-gcm-android --save`\n\n- In `android/build.gradle`\n```gradle\ndependencies {\n    classpath 'com.android.tools.build:gradle:1.3.1'\n    classpath 'com.google.gms:google-services:1.5.0-beta3' // \u003c- Add this line\n```\n\n- In `android/settings.gradle`, add\n```gradle\ninclude ':RNGcmAndroid', ':app'\nproject(':RNGcmAndroid').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-gcm-android/android')\n\ninclude ':react-native-system-notification'\nproject(':react-native-system-notification').projectDir = new File(settingsDir, '../node_modules/react-native-system-notification/android')\n```\n\n- In `android/app/build.gradle`\n```gradle\napply plugin: \"com.android.application\"\napply plugin: 'com.google.gms.google-services'           // \u003c- Add this line\n...\ndependencies {\n    compile fileTree(dir: \"libs\", include: [\"*.jar\"])\n    compile \"com.android.support:appcompat-v7:23.0.1\"\n    compile \"com.facebook.react:react-native:0.16.+\"\n    compile 'com.google.android.gms:play-services-gcm:8.3.0' // \u003c- Add this line\n    compile project(':RNGcmAndroid')                         // \u003c- Add this line\n    compile project(':react-native-system-notification')     // \u003c- Add this line\n}\n```\n\n- In `android/app/src/main/AndroidManifest.xml`, add these lines, be sure to change `com.xxx.yyy` to your package\n```xml\n\u003cuses-permission android:name=\"com.google.android.c2dm.permission.RECEIVE\" /\u003e\n\u003cuses-permission android:name=\"android.permission.WAKE_LOCK\" /\u003e\n\u003cuses-permission android:name=\"com.google.android.c2dm.permission.SEND\" /\u003e\n\u003cuses-permission android:name=\"android.permission.GET_ACCOUNTS\" /\u003e\n\u003cuses-permission android:name=\"android.permission.GET_TASKS\" /\u003e \n\u003cuses-permission android:name=\"android.permission.RECEIVE_BOOT_COMPLETED\"/\u003e\n\n\u003cpermission\n  android:name=\"com.xxx.yyy.permission.C2D_MESSAGE\"\n  android:protectionLevel=\"signature\" /\u003e\n\u003cuses-permission android:name=\"com.xxx.yyy.permission.C2D_MESSAGE\" /\u003e\n\u003cuses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\" /\u003e\n\u003cuses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" /\u003e\n\u003cuses-permission android:name=\"android.permission.VIBRATE\"\u003e\u003c/uses-permission\u003e\n\n...\n\n\u003capplication\n  android:theme=\"@style/AppTheme\"\u003e\n\n  ...\n  \u003cmeta-data\n    android:name=\"com.google.android.gms.version\"\n    android:value=\"@integer/google_play_services_version\" /\u003e\n\n  \u003creceiver\n    android:name=\"com.google.android.gms.gcm.GcmReceiver\"\n    android:exported=\"true\"\n    android:permission=\"com.google.android.c2dm.permission.SEND\" \u003e\n    \u003cintent-filter\u003e\n      \u003caction android:name=\"com.google.android.c2dm.intent.RECEIVE\" /\u003e\n      \u003ccategory android:name=\"com.xxx.yyy\" /\u003e\n    \u003c/intent-filter\u003e\n  \u003c/receiver\u003e\n  \u003cservice android:name=\"com.oney.gcm.GcmRegistrationService\"/\u003e\n  \u003cservice android:name=\"com.oney.gcm.BackgroundService\"\u003e\u003c/service\u003e\n\n  \u003cservice\n    android:name=\"com.oney.gcm.RNGcmListenerService\"\n    android:exported=\"false\" \u003e\n    \u003cintent-filter\u003e\n      \u003caction android:name=\"com.google.android.c2dm.intent.RECEIVE\" /\u003e\n    \u003c/intent-filter\u003e\n  \u003c/service\u003e\n  \u003creceiver\n    android:exported=\"false\"\n    android:name=\"com.oney.gcm.GcmBroadcastReceiver\"\u003e\n    \u003cintent-filter\u003e\n      \u003caction android:name=\"com.oney.gcm.GCMReceiveNotification\" /\u003e\n      \u003c/intent-filter\u003e\n  \u003c/receiver\u003e\n\n  \u003creceiver android:name=\"io.neson.react.notification.NotificationEventReceiver\" /\u003e\n  \u003creceiver android:name=\"io.neson.react.notification.NotificationPublisher\" /\u003e\n  \u003creceiver android:name=\"io.neson.react.notification.SystemBootEventReceiver\"\u003e\n    \u003cintent-filter\u003e\n      \u003caction android:name=\"android.intent.action.BOOT_COMPLETED\"\u003e\u003c/action\u003e\n    \u003c/intent-filter\u003e\n  \u003c/receiver\u003e\n  ...\n```\n- In `android/app/src/main/java/com/testoe/MainActivity.java`\n```java\nimport com.oney.gcm.GcmPackage;                             // \u003c- Add this line\nimport io.neson.react.notification.NotificationPackage;     // \u003c- Add this line\n    ...\n        .addPackage(new MainReactPackage())\n        .addPackage(new GcmPackage())                       // \u003c- Add this line\n        .addPackage(new NotificationPackage(this))          // \u003c- Add this line\n```\n\n### GCM API KEY\nBy following [Cloud messaging](https://developers.google.com/cloud-messaging/android/client), you can get `google-services.json` file and place it in `android/app` directory\n\n### Usage\n\n```javascript\n'use strict';\n\nvar React = require('react-native');\nvar {\n  AppRegistry,\n  View,\n  DeviceEventEmitter,\n} = React;\n\nvar GcmAndroid = require('react-native-gcm-android');\nimport Notification from 'react-native-system-notification';\n\nif (GcmAndroid.launchNotification) {\n  var notification = GcmAndroid.launchNotification;\n  var info = JSON.parse(notification.info);\n  Notification.create({\n    subject: info.subject,\n    message: info.message,\n  });\n  GcmAndroid.stopService();\n} else {\n\n  var {Router, Route, Schema, Animations, TabBar} = require('react-native-router-flux');\n  var YourApp = React.createClass({\n    componentDidMount: function() {\n      GcmAndroid.addEventListener('register', function(token){\n        console.log('send gcm token to server', token);\n      });\n      GcmAndroid.addEventListener('registerError', function(error){\n        console.log('registerError', error.message);\n      });\n      GcmAndroid.addEventListener('notification', function(notification){\n        console.log('receive gcm notification', notification);\n        var info = JSON.parse(notification.data.info);\n        if (!GcmAndroid.isInForeground) {\n          Notification.create({\n            subject: info.subject,\n            message: info.message,\n          });\n        }\n      });\n\n      DeviceEventEmitter.addListener('sysNotificationClick', function(e) {\n        console.log('sysNotificationClick', e);\n      });\n\n      GcmAndroid.requestPermissions();\n    },\n    render: function() {\n      return (\n        ...\n      );\n    }\n  });\n\n  AppRegistry.registerComponent('YourApp', () =\u003e YourApp);\n}\n```\n\n* There are two situations.\n##### The app is running on the foreground or background.\n`GcmAndroid.launchNotification` is `null`, you can get notification in `GcmAndroid.addEventListener('notification'` listenter.\n##### The app is killed/closed\n`GcmAndroid.launchNotification` is your GCM data. You can create notification with resolving the data by using [react-native-system-notification module](https://github.com/Neson/react-native-system-notification).\n\n* You can get info when clicking notification in `DeviceEventEmitter.addListener('sysNotificationClick'`. See [react-native-system-notification](https://github.com/Neson/react-native-system-notification) to get more informations about how to create notification \n\n## Troubleshoot\n\n- Do not add `multiDexEnabled true` in `android/app/build.gradle` even encounter `com.android.dex.DexException: Multiple dex files...` failure.\n- Make sure to install Google Play service in Genymotion simulator before testing.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foney%2Freact-native-gcm-android","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foney%2Freact-native-gcm-android","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foney%2Freact-native-gcm-android/lists"}