Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.45 KB | None | 0 0
  1. public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
  2.  
  3. private static final String TAG = "MyFirebaseIIDService";
  4.  
  5. /**
  6. * Called if InstanceID token is updated. This may occur if the security of
  7. * the previous token had been compromised. Note that this is called when the InstanceID token
  8. * is initially generated so this is where you would retrieve the token.
  9. */
  10. // [START refresh_token]
  11. @Override
  12. public void onTokenRefresh() {
  13. // Get updated InstanceID token.
  14. String refreshedToken = FirebaseInstanceId.getInstance().getToken();
  15. Log.d(TAG, "Refreshed token: " + refreshedToken);
  16.  
  17. // If you want to send messages to this application instance or
  18. // manage this apps subscriptions on the server side, send the
  19. // Instance ID token to your app server.
  20. sendRegistrationToServer(refreshedToken);
  21. }
  22. // [END refresh_token]
  23.  
  24. /**
  25. * Persist token to third-party servers.
  26. *
  27. * Modify this method to associate the user's FCM InstanceID token with any server-side account
  28. * maintained by your application.
  29. *
  30. * @param token The new token.
  31. */
  32. private void sendRegistrationToServer(String token) {
  33. // TODO: Implement this method to send token to your app server.
  34. }
  35. }
  36.  
  37. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  38.  
  39. private static final String TAG = "MyFirebaseMsgService";
  40.  
  41. /**
  42. * Called when message is received.
  43. *
  44. * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
  45. */
  46. // [START receive_message]
  47. @Override
  48. public void onMessageReceived(RemoteMessage remoteMessage) {
  49. // [START_EXCLUDE]
  50. // There are two types of messages data messages and notification messages. Data messages are handled
  51. // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
  52. // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
  53. // is in the foreground. When the app is in the background an automatically generated notification is displayed.
  54. // When the user taps on the notification they are returned to the app. Messages containing both notification
  55. // and data payloads are treated as notification messages. The Firebase console always sends notification
  56. // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
  57. // [END_EXCLUDE]
  58.  
  59. // TODO(developer): Handle FCM messages here.
  60. Log.d(TAG, "From: " + remoteMessage.getFrom());
  61.  
  62. //Toast.makeText(getApplicationContext(),"FROM "+remoteMessage.getFrom(),Toast.LENGTH_LONG).show();
  63. // Check if message contains a data payload.
  64. if (remoteMessage.getData().size() > 0) {
  65. Log.d(TAG, "Message data payload: " + remoteMessage.getData());
  66. //Toast.makeText(getApplicationContext(),"MESSAGE: "+remoteMessage.getData(),Toast.LENGTH_LONG).show();
  67. }
  68.  
  69. // Check if message contains a notification payload.
  70. if (remoteMessage.getNotification() != null) {
  71. //MainActivity.TestMethod();
  72. Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
  73. //Toast.makeText(getApplicationContext(),"MESSAGE: "+remoteMessage.getData(),Toast.LENGTH_LONG).show();
  74. }
  75.  
  76. // Also if you intend on generating your own notifications as a result of a received FCM
  77. // message, here is where that should be initiated. See sendNotification method below.
  78. sendNotification(remoteMessage.getNotification().getBody());
  79. }
  80. // [END receive_message]
  81.  
  82. /**
  83. * Create and show a simple notification containing the received FCM message.
  84. *
  85. * @param messageBody FCM message body received.
  86. */
  87. private void sendNotification(String messageBody) {
  88. Intent intent = new Intent(this, MainActivity.class);
  89. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  90. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
  91. PendingIntent.FLAG_ONE_SHOT);
  92.  
  93. Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  94. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
  95. .setSmallIcon(R.drawable.ic_stat_ic_notification)
  96. .setContentTitle("FCM Message")
  97. .setContentText(messageBody)
  98. .setAutoCancel(true)
  99. .setSound(defaultSoundUri)
  100. .setContentIntent(pendingIntent);
  101.  
  102. NotificationManager notificationManager =
  103. (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  104.  
  105. notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
  106. }
  107. }
  108.  
  109. public class MainActivity extends AppCompatActivity {
  110.  
  111. private static final String TAG = "MainActivity";
  112.  
  113. @Override
  114. protected void onCreate(Bundle savedInstanceState) {
  115. super.onCreate(savedInstanceState);
  116. setContentView(R.layout.activity_main);
  117.  
  118. // If a notification message is tapped, any data accompanying the notification
  119. // message is available in the intent extras. In this sample the launcher
  120. // intent is fired when the notification is tapped, so any accompanying data would
  121. // be handled here. If you want a different intent fired, set the click_action
  122. // field of the notification message to the desired intent. The launcher intent
  123. // is used when no click_action is specified.
  124. //
  125. // Handle possible data accompanying notification message.
  126. // [START handle_data_extras]
  127. if (getIntent().getExtras() != null) {
  128. for (String key : getIntent().getExtras().keySet()) {
  129. String value = getIntent().getExtras().getString(key);
  130. Log.d(TAG, "Key: " + key + " Value: " + value);
  131. }
  132. }
  133. // [END handle_data_extras]
  134.  
  135. Button subscribeButton = (Button) findViewById(R.id.subscribeButton);
  136. subscribeButton.setOnClickListener(new View.OnClickListener() {
  137. @Override
  138. public void onClick(View v) {
  139. // [START subscribe_topics]
  140. FirebaseMessaging.getInstance().subscribeToTopic("news");
  141. // [END subscribe_topics]
  142.  
  143. // Log and toast
  144. String msg = getString(R.string.msg_subscribed);
  145. Log.d(TAG, msg);
  146. Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
  147. }
  148. });
  149.  
  150. Button logTokenButton = (Button) findViewById(R.id.logTokenButton);
  151. logTokenButton.setOnClickListener(new View.OnClickListener() {
  152. @Override
  153. public void onClick(View v) {
  154. // Get token
  155. String token = FirebaseInstanceId.getInstance().getToken();
  156.  
  157. // Log and toast
  158. String msg = getString(R.string.msg_token_fmt, token);
  159. Log.d(TAG, msg);
  160. Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
  161. }
  162. });
  163. }
  164. }
  165.  
  166. // Top-level build file where you can add configuration options commonto `all sub-projects/modules.`
  167.  
  168. buildscript {
  169. repositories {
  170. jcenter()
  171. mavenLocal()
  172. }
  173. dependencies {
  174. classpath 'com.android.tools.build:gradle:2.1.3'
  175. classpath 'com.google.gms:google-services:3.0.0'
  176.  
  177. // NOTE: Do not place your application dependencies here; they belong
  178. // in the individual module build.gradle files
  179. }
  180. }
  181.  
  182. allprojects {
  183. repositories {
  184. jcenter()
  185. mavenLocal()
  186. }
  187. }
  188.  
  189. apply plugin: 'com.android.application'
  190.  
  191. android {
  192. compileSdkVersion 24
  193. buildToolsVersion "24.0.0"
  194.  
  195. defaultConfig {
  196. applicationId "com.google.firebase.quickstart.fcm"
  197. minSdkVersion 15
  198. targetSdkVersion 24
  199. versionCode 1
  200. versionName "1.0"
  201.  
  202. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  203. }
  204.  
  205. buildTypes {
  206. release {
  207. minifyEnabled false
  208. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  209. }
  210. }
  211.  
  212. packagingOptions {
  213. exclude 'LICENSE.txt'
  214. }
  215. }
  216.  
  217. dependencies {
  218. compile fileTree(dir: 'libs', include: ['*.jar'])
  219.  
  220. // Testing dependencies
  221. androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
  222. androidTestCompile 'com.android.support.test:runner:0.2'
  223. androidTestCompile 'com.android.support:support-annotations:24.2.0'
  224. compile 'com.android.support:appcompat-v7:24.2.0'
  225. compile 'com.google.firebase:firebase-messaging:9.4.0'
  226. compile 'com.google.firebase:firebase-core:9.4.0'
  227. compile 'com.google.android.gms:play-services-gcm:9.4.0'
  228. compile 'com.android.support:design:24.2.0'
  229. }
  230.  
  231. apply plugin: 'com.google.gms.google-services'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement