1<!DOCTYPE html> 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87<html> 88<head> 89 90 91<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 92<meta name="viewport" content="width=device-width" /> 93 94<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" /> 95<title>GoogleCloudMessaging | Android Developers</title> 96 97<!-- STYLESHEETS --> 98<link rel="stylesheet" 99href="//fonts.googleapis.com/css?family=Roboto+Condensed"> 100<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:light,regular,medium,thin,italic,mediumitalic,bold" 101 title="roboto"> 102<link href="/assets/css/default.css?v=2" rel="stylesheet" type="text/css"> 103 104 105 106<!-- JAVASCRIPT --> 107<script src="//www.google.com/jsapi" type="text/javascript"></script> 108<script src="/assets/js/android_3p-bundle.js" type="text/javascript"></script> 109<script type="text/javascript"> 110 var toRoot = "/"; 111 var metaTags = []; 112 var devsite = false; 113</script> 114<script src="/assets/js/docs.js?v=2" type="text/javascript"></script> 115 116<script> 117 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ 118 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), 119 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) 120 })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); 121 122 ga('create', 'UA-5831155-1', 'android.com'); 123 ga('create', 'UA-49880327-2', 'android.com', {'name': 'universal'}); // New tracker); 124 ga('send', 'pageview'); 125 ga('universal.send', 'pageview'); // Send page view for new tracker. 126</script> 127 128</head> 129 130<body class="gc-documentation google 131 develop" itemscope itemtype="http://schema.org/Article"> 132 <div id="doc-api-level" class="" style="display:none"></div> 133 <a name="top"></a> 134 135 136<a name="top"></a> 137 138 <!-- Header --> 139 <div id="header-wrapper"> 140 <div id="header"> 141 <div class="wrap" id="header-wrap"> 142 <div class="col-3 logo"> 143 <a href="/index.html"> 144 <img src="/assets/images/dac_logo.png" 145 srcset="/assets/images/dac_logo@2x.png 2x" 146 width="123" height="25" alt="Android Developers" /> 147 </a> 148 <div class="btn-quicknav" id="btn-quicknav"> 149 <a href="#" class="arrow-inactive">Quicknav</a> 150 <a href="#" class="arrow-active">Quicknav</a> 151 </div> 152 </div> 153 <ul class="nav-x col-9"> 154 <li class="design"> 155 <a href="/design/index.html" 156 zh-tw-lang="設計" 157 zh-cn-lang="设计" 158 ru-lang="Проектирование" 159 ko-lang="디자인" 160 ja-lang="設計" 161 es-lang="Diseñar" 162 >Design</a></li> 163 <li class="develop"><a href="/develop/index.html" 164 zh-tw-lang="開發" 165 zh-cn-lang="开发" 166 ru-lang="Разработка" 167 ko-lang="개발" 168 ja-lang="開発" 169 es-lang="Desarrollar" 170 >Develop</a></li> 171 <li class="distribute last"><a href="/distribute/index.html" 172 zh-tw-lang="發佈" 173 zh-cn-lang="分发" 174 ru-lang="Распространение" 175 ko-lang="배포" 176 ja-lang="配布" 177 es-lang="Distribuir" 178 >Distribute</a></li> 179 </ul> 180 181 182 183 184<div class="menu-container"> 185 <div class="moremenu"> 186 <div id="more-btn"></div> 187 </div> 188 <div class="morehover" id="moremenu"> 189 <div class="top"></div> 190 <div class="mid"> 191 <div class="header">Links</div> 192 <ul> 193 <li><a href="https://play.google.com/apps/publish/" target="_googleplay">Google Play Developer Console</a></li> 194 <li><a href="http://android-developers.blogspot.com/">Android Developers Blog</a></li> 195 <li><a href="/about/index.html">About Android</a></li> 196 </ul> 197 <div class="header">Android Sites</div> 198 <ul> 199 <li><a href="http://www.android.com">Android.com</a></li> 200 <li class="active"><a>Android Developers</a></li> 201 <li><a href="http://source.android.com">Android Open Source Project</a></li> 202 </ul> 203 204 205 206 <div class="header">Language</div> 207 <div id="language" class="locales"> 208 <select name="language" onChange="changeLangPref(this.value, true)"> 209 <option value="en">English</option> 210 <option value="es">Español</option> 211 <option value="ja">日本語</option> 212 <option value="ko">한국어</option> 213 <option value="ru">Русский</option> 214 <option value="zh-cn">中文 (中国)</option> 215 <option value="zh-tw">中文 (台灣)</option> 216 </select> 217 </div> 218 <script type="text/javascript"> 219 <!-- 220 loadLangPref(); 221 //--> 222 </script> 223 224 225 <br class="clearfix" /> 226 </div><!-- end 'mid' --> 227 <div class="bottom"></div> 228 </div><!-- end 'moremenu' --> 229 230 <div class="search" id="search-container"> 231 <div class="search-inner"> 232 <div id="search-btn"></div> 233 <div class="left"></div> 234 <form onsubmit="return submit_search()"> 235 <input id="search_autocomplete" type="text" value="" autocomplete="off" name="q" 236 onfocus="search_focus_changed(this, true)" onblur="search_focus_changed(this, false)" 237 onkeydown="return search_changed(event, true, '/')" 238 onkeyup="return search_changed(event, false, '/')" /> 239 </form> 240 <div class="right"></div> 241 <a class="close hide">close</a> 242 <div class="left"></div> 243 <div class="right"></div> 244 </div><!-- end search-inner --> 245 </div><!-- end search-container --> 246 247 <div class="search_filtered_wrapper reference"> 248 <div class="suggest-card reference no-display"> 249 <ul class="search_filtered"> 250 </ul> 251 </div> 252 </div> 253 254 <div class="search_filtered_wrapper docs"> 255 <div class="suggest-card dummy no-display"> </div> 256 <div class="suggest-card develop no-display"> 257 <ul class="search_filtered"> 258 </ul> 259 <div class="child-card guides no-display"> 260 </div> 261 <div class="child-card training no-display"> 262 </div> 263 <div class="child-card samples no-display"> 264 </div> 265 </div> 266 <div class="suggest-card design no-display"> 267 <ul class="search_filtered"> 268 </ul> 269 </div> 270 <div class="suggest-card distribute no-display"> 271 <ul class="search_filtered"> 272 </ul> 273 </div> 274 </div> 275</div><!-- end menu-container (search and menu widget) --> 276 277 278 279 <!-- Expanded quicknav --> 280 <div id="quicknav" class="col-13"> 281 <ul> 282 <li class="about"> 283 <ul> 284 <li><a href="/about/index.html">About</a></li> 285 <li><a href="/wear/index.html">Wear</a></li> 286 <li><a href="/tv/index.html">TV</a></li> 287 <li><a href="/auto/index.html">Auto</a></li> 288 </ul> 289 </li> 290 <li class="design"> 291 <ul> 292 <li><a href="/design/index.html">Get Started</a></li> 293 <li><a href="/design/devices.html">Devices</a></li> 294 <li><a href="/design/style/index.html">Style</a></li> 295 <li><a href="/design/patterns/index.html">Patterns</a></li> 296 <li><a href="/design/building-blocks/index.html">Building Blocks</a></li> 297 <li><a href="/design/downloads/index.html">Downloads</a></li> 298 <li><a href="/design/videos/index.html">Videos</a></li> 299 </ul> 300 </li> 301 <li class="develop"> 302 <ul> 303 <li><a href="/training/index.html" 304 zh-tw-lang="訓練課程" 305 zh-cn-lang="培训" 306 ru-lang="Курсы" 307 ko-lang="교육" 308 ja-lang="トレーニング" 309 es-lang="Capacitación" 310 >Training</a></li> 311 <li><a href="/guide/index.html" 312 zh-tw-lang="API 指南" 313 zh-cn-lang="API 指南" 314 ru-lang="Руководства по API" 315 ko-lang="API 가이드" 316 ja-lang="API ガイド" 317 es-lang="Guías de la API" 318 >API Guides</a></li> 319 <li><a href="/reference/packages.html" 320 zh-tw-lang="參考資源" 321 zh-cn-lang="参考" 322 ru-lang="Справочник" 323 ko-lang="참조문서" 324 ja-lang="リファレンス" 325 es-lang="Referencia" 326 >Reference</a></li> 327 <li><a href="/sdk/index.html" 328 zh-tw-lang="相關工具" 329 zh-cn-lang="工具" 330 ru-lang="Инструменты" 331 ko-lang="도구" 332 ja-lang="ツール" 333 es-lang="Herramientas" 334 >Tools</a> 335 </li> 336 <li><a href="/google/index.html">Google Services</a> 337 </li> 338 339 <li><a href="/samples/index.html">Samples</a> 340 </li> 341 342 </ul> 343 </li> 344 <li class="distribute last"> 345 <ul> 346 <li><a href="/distribute/googleplay/index.html">Google Play</a></li> 347 <li><a href="/distribute/essentials/index.html">Essentials</a></li> 348 <li><a href="/distribute/users/index.html">Get Users</a></li> 349 <li><a href="/distribute/engage/index.html">Engage & Retain</a></li> 350 <li><a href="/distribute/monetize/index.html">Monetize</a></li> 351 <li><a href="/distribute/tools/index.html">Tools & Reference</a></li> 352 <li><a href="/distribute/stories/index.html">Developer Stories</a></li> 353 </ul> 354 </li> 355 </ul> 356 </div><!-- /Expanded quicknav --> 357 </div><!-- end header-wrap.wrap --> 358 </div><!-- end header --> 359 360 361 <!-- Secondary x-nav --> 362 <div id="nav-x"> 363 <div class="wrap"> 364 <ul class="nav-x col-9 develop" style="width:100%"> 365 <li class="training"><a href="/training/index.html" 366 zh-tw-lang="訓練課程" 367 zh-cn-lang="培训" 368 ru-lang="Курсы" 369 ko-lang="교육" 370 ja-lang="トレーニング" 371 es-lang="Capacitación" 372 >Training</a></li> 373 <li class="guide"><a href="/guide/index.html" 374 zh-tw-lang="API 指南" 375 zh-cn-lang="API 指南" 376 ru-lang="Руководства по API" 377 ko-lang="API 가이드" 378 ja-lang="API ガイド" 379 es-lang="Guías de la API" 380 >API Guides</a></li> 381 <li class="reference"><a href="/reference/packages.html" 382 zh-tw-lang="參考資源" 383 zh-cn-lang="参考" 384 ru-lang="Справочник" 385 ko-lang="참조문서" 386 ja-lang="リファレンス" 387 es-lang="Referencia" 388 >Reference</a></li> 389 <li class="tools"><a href="/sdk/index.html" 390 zh-tw-lang="相關工具" 391 zh-cn-lang="工具" 392 ru-lang="Инструменты" 393 ko-lang="도구" 394 ja-lang="ツール" 395 es-lang="Herramientas" 396 >Tools</a></li> 397 <li class="google"><a href="/google/index.html" 398 >Google Services</a> 399 </li> 400 401 <li class="samples"><a href="/samples/index.html" 402 >Samples</a> 403 </li> 404 405 </ul> 406 </div> 407 </div> 408 <!-- /Sendondary x-nav DEVELOP --> 409 410 411 412 <div id="searchResults" class="wrap" style="display:none;"> 413 <h2 id="searchTitle">Results</h2> 414 <div id="leftSearchControl" class="search-control">Loading...</div> 415 </div> 416 </div> <!--end header-wrapper --> 417 418 <div id="sticky-header"> 419 <div> 420 <a class="logo" href="#top"></a> 421 <a class="top" href="#top"></a> 422 <ul class="breadcrumb"> 423 424 <li class="current">GoogleCloudMessaging</li> 425 </ul> 426 </div> 427 </div> 428 429 430 431 432 433 434 <div class="wrap clearfix" id="body-content"> 435 <div class="col-4" id="side-nav" itemscope itemtype="http://schema.org/SiteNavigationElement"> 436 <div id="devdoc-nav" class="scroll-pane"> 437 438 439 440<ul id="nav"> 441 442 <li class="nav-section"> 443 <div class="nav-section-header empty"><a href="/google/index.html"> 444 <span class="en">Overview</span> 445 </a></div> 446 </li> 447 448 <li class="nav-section"> 449 <div class="nav-section-header empty"><a href="/google/play-services/games.html"> 450 <span class="en">Games</span> 451 </a></div> 452 </li> 453 <li class="nav-section"> 454 <div class="nav-section-header empty"><a href="/google/play-services/location.html"> 455 <span class="en">Location</span> 456 </a></div> 457 </li> 458 <li class="nav-section"> 459 <div class="nav-section-header empty"><a href="/google/play-services/plus.html"> 460 <span class="en">Google+</span> 461 </a></div> 462 </li> 463 <li class="nav-section"> 464 <div class="nav-section-header empty"><a href="/google/play-services/maps.html"> 465 <span class="en">Maps</span> 466 </a></div> 467 </li> 468 <li class="nav-section"> 469 <div class="nav-section-header empty"><a href="/google/play-services/drive.html"> 470 <span class="en">Drive</span> 471 </a></div> 472 </li> 473 <li class="nav-section"> 474 <div class="nav-section-header empty"><a href="/google/play-services/cast.html"> 475 <span class="en">Cast</span> 476 </a></div> 477 </li> 478 <li class="nav-section"> 479 <div class="nav-section-header"><a href="/google/play-services/ads.html"> 480 <span class="en">Ads</span></a> 481 </div> 482 <ul> 483 <li><a href="/google/play-services/id.html"> 484 <span class="en">Advertising ID</span></a> 485 </li> 486 </ul> 487 </li> 488 <li class="nav-section"> 489 <div class="nav-section-header empty"><a href="/google/play-services/wallet.html"> 490 <span class="en">Wallet</span> 491 </a></div> 492 </li> 493 494 495 <li class="nav-section"> 496 <div class="nav-section-header"><a href="/google/play-services/index.html"> 497 <span class="en">Google Play Services</span></a> 498 </div> 499 <ul> 500 <li><a href="/google/play-services/setup.html"> 501 <span class="en">Setting Up Google Play Services</span></a> 502 </li> 503 <li class="nav-section"> 504 <div class="nav-section-header"><a href="/google/auth/api-client.html"> 505 <span class="en">Accessing Google APIs</span></a> 506 </div> 507 <ul> 508 <li> 509 <a href="/google/auth/http-auth.html"> 510 <span class="en">Authorizing with Google for REST APIs</span> 511 </a> 512 </li> 513 </ul> 514 </li> 515 <li id="gms-tree-list" class="nav-section"> 516 <div class="nav-section-header"> 517 <a href="/reference/gms-packages.html"> 518 <span class="en">Reference</span> 519 </a> 520 <div> 521 </li> 522 </ul> 523 </li> 524 525 526 <li class="nav-section"> 527 <div class="nav-section-header"><a href="/google/play/billing/index.html"> 528 <span class="en">Google Play In-app Billing</span></a> 529 </div> 530 <ul> 531 <li><a href="/google/play/billing/billing_overview.html"> 532 <span class="en">Overview</span></a> 533 </li> 534 <li class="nav-section"><div class="nav-section-header"><a href="/google/play/billing/api.html"> 535 <span class="en">Version 3 API</span></a></div> 536 <ul> 537 <li><a href="/google/play/billing/billing_integrate.html"> 538 <span class="en">Implementing the API</span></a></li> 539 <li><a href="/google/play/billing/billing_reference.html"> 540 <span class="en">Reference</span></a></li> 541 </ul> 542 </li> 543 <li class="nav-section"><div class="nav-section-header"><a href="/google/play/billing/v2/api.html"> 544 <span class="en">Version 2 API</span></a></div> 545 <ul> 546 <li><a href="/google/play/billing/v2/billing_integrate.html"> 547 <span class="en">Implementing the API</span></a></li> 548 <li><a href="/google/play/billing/v2/billing_subscriptions.html"> 549 <span class="en">Subscriptions</span></a></li> 550 <li><a href="/google/play/billing/v2/billing_reference.html"> 551 <span class="en">Reference</span></a></li> 552 </ul> 553 </li> 554 <li><a href="/google/play/billing/billing_subscriptions.html"> 555 <span class="en">Subscriptions</span></a> 556 </li> 557 <li><a href="/google/play/billing/billing_best_practices.html"> 558 <span class="en">Security and Design</span></a> 559 </li> 560 <li><a href="/google/play/billing/billing_testing.html"> 561 <span class="en">Testing In-app Billing</span></a> 562 </li> 563 <li><a href="/google/play/billing/billing_admin.html"> 564 <span class="en">Administering In-app Billing</span></a> 565 </li> 566 <li><a href="/google/play/billing/versions.html"> 567 <span class="en">Version Notes</span></a> 568 </li> 569 </ul> 570 </li> 571 572 573 574 <li class="nav-section"> 575 <div class="nav-section-header"><a href="/google/gcm/index.html"> 576 <span class="en">Google Cloud Messaging</span></a> 577 </div> 578 <ul> 579 <li><a href="/google/gcm/gcm.html"> 580 <span class="en">Overview</span></a> 581 </li> 582 <li><a href="/google/gcm/gs.html"> 583 <span class="en">Getting Started</span></a> 584 </li> 585 <li><a href="/google/gcm/client.html"> 586 <span class="en">Implementing GCM Client</span></a> 587 </li> 588 <li class="nav-section"><div class="nav-section-header"><a href="/google/gcm/server.html"> 589 <span class="en">Implementing GCM Server</span></a></div> 590 <ul> 591 <li><a href="/google/gcm/ccs.html"> 592 <span class="en">CCS (XMPP)</span></a></li> 593 <li><a href="/google/gcm/http.html"> 594 <span class="en">HTTP</span></a></li> 595 </ul> 596 </li> 597 <li><a href="/google/gcm/notifications.html"> 598 <span class="en">User Notifications</span></a> 599 </li> 600 <li><a href="/google/gcm/adv.html"> 601 <span class="en">Advanced Topics</span></a> 602 </li> 603 <li><a href="/google/gcm/c2dm.html"> 604 <span class="en">Migration</span></a> 605 </li> 606 <li id="gcm-tree-list" class="nav-section"> 607 <div class="nav-section-header"> 608 <a href="/reference/gcm-packages.html"> 609 <span class="en">Reference</span> 610 </a> 611 <div> 612 </li> 613 </ul> 614 </li> 615 616 <li class="nav-section"> 617 <div class="nav-section-header empty"><a href="/google/gcs/index.html"> 618 <span class="en">Google Cloud Save</span></a> 619 </div> 620 </li> 621 622 <li class="nav-section"> 623 <div class="nav-section-header"><a href="/google/play/dist.html"> 624 <span class="en">Google Play Distribution</span></a> 625 </div> 626 <ul> 627 <li><a href="/google/play/filters.html"> 628 <span class="en">Filters on Google Play</span></a> 629 </li> 630 <li><a href="/google/play/billing/gp-purchase-status-api.html"> 631 <span class="en">Google Play Developer API</span></a> 632 </li> 633 <li><a href="/google/play/publishing/multiple-apks.html"> 634 <span class="en">Multiple APK Support</span></a> 635 </li> 636 <li><a href="/google/play/expansion-files.html"> 637 <span class="en">APK Expansion Files</span></a> 638 </li> 639 <li class="nav-section"> 640 <div class="nav-section-header"><a href="/google/play/licensing/index.html"> 641 <span class="en">Application Licensing</span></a> 642 </div> 643 <ul> 644 <li><a href="/google/play/licensing/overview.html"> 645 <span class="en">Licensing Overview</span></a> 646 </li> 647 <li><a href="/google/play/licensing/setting-up.html"> 648 <span class="en">Setting Up for Licensing</span></a> 649 </li> 650 <li><a href="/google/play/licensing/adding-licensing.html"> 651 <span class="en">Adding Licensing to Your App</span></a> 652 </li> 653 <li><a href="/google/play/licensing/licensing-reference.html"> 654 <span class="en">Licensing Reference</span></a> 655 </li> 656 </ul> 657 </li> 658 659 <li class="nav-section"> 660 <div class="nav-section-header"><a href="/google/backup/index.html"> 661 Android Backup Service</a> 662 </div> 663 <ul> 664 <li><a href="/google/backup/signup.html"> 665 Register</a> 666 </li> 667 </ul> 668 </li> 669 670 </ul> 671 672</li> 673 674 675 676</ul> 677 678<script type="text/javascript"> 679<!-- 680 buildToggleLists(); 681 changeNavLang(getLangPref()); 682//--> 683</script> 684 685 686 687 688 </div> 689 <script type="text/javascript"> 690 showGoogleRefTree(); 691 692 </script> 693 </div> <!-- end side-nav --> 694 <script> 695 $(document).ready(function() { 696 scrollIntoView("devdoc-nav"); 697 }); 698 </script> 699 700 701 702 703 704 705<div class="col-12" id="doc-col"> 706 707<div id="api-info-block"> 708 709 710 711 712 713 714 715 716 717 718 719<div class="sum-details-links"> 720 721Summary: 722 723 724 725 726 727 <a href="#constants">Constants</a> 728 729 730 731 732 733 734 | <a href="#pubctors">Ctors</a> 735 736 737 738 739 | <a href="#pubmethods">Methods</a> 740 741 742 743 744 | <a href="#inhmethods">Inherited Methods</a> 745 746| <a href="#" onclick="return toggleAllClassInherited()" id="toggleAllClassInherited">[Expand All]</a> 747 748</div><!-- end sum-details-links --> 749<div class="api-level"> 750 751 752 753 754</div> 755</div><!-- end api-info-block --> 756 757 758<!-- ======== START OF CLASS DATA ======== --> 759 760<div id="jd-header"> 761 public 762 763 764 765 class 766<h1 itemprop="name">GoogleCloudMessaging</h1> 767 768 769 770 771 extends Object<br/> 772 773 774 775 776 777 778 779 780 781</div><!-- end header --> 782 783<div id="naMessage"></div> 784 785<div id="jd-content" class="api apilevel-"> 786<table class="jd-inheritance-table"> 787 788 789 <tr> 790 791 <td colspan="2" class="jd-inheritance-class-cell">java.lang.Object</td> 792 </tr> 793 794 795 <tr> 796 797 <td class="jd-inheritance-space"> ↳</td> 798 799 <td colspan="1" class="jd-inheritance-class-cell">com.google.android.gms.gcm.GoogleCloudMessaging</td> 800 </tr> 801 802 803</table> 804 805 806 807 808 809 810 811<div class="jd-descr"> 812 813 814<h2>Class Overview</h2> 815<p itemprop="articleBody"><p>The class you use to write a GCM-enabled client application that runs on an Android device. 816 Client applications can receive GCM messages and optionally send messages of their own back to 817 the server. 818 819 <p>This class requires Google Play services version 3.1 or higher. For a 820 detailed discussion of how to write a GCM client app, see 821 <a href="http://developer.android.com/google/gcm/client.html"> 822 Implementing GCM Client</a>. 823 824 <p>To send or receive messages, your application first needs to get a registration ID. The 825 registration ID identifies the device and application, and also determines which 3rd-party 826 application servers are allowed to send messages to this application instance. 827 828 <p>To get a registration ID, you must supply one or more sender IDs. A sender ID is a project 829 number you acquire from the API console, as described in 830 <a href="http://developer.android.com/google/gcm/gs.html">Getting Started</a>. The sender ID is 831 used in the registration process to identify a 3rd-party application server that is permitted to 832 send messages to the device. The following snippet shows you how to call the 833 <code><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#register(java.lang.String...)">register()</a></code> method. For a more comprehensive example, see 834 <a href="http://developer.android.com/google/gcm/client.html">Implementing GCM Client</a>. 835 836 <pre> 837 String SENDER_ID = "My-Sender-ID"; 838 GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); 839 String registrationId = gcm.register(SENDER_ID); 840 // Upload the registration ID to your own server 841 // The request to your server should be authenticated if your app is using accounts. 842 </pre> 843 844 <p>In order to receive GCM messages, you need to declare a permission and a 845 <code>BroadcastReceiver</code> in your manifest. This is a backward-compatible subset of what was 846 required in previous versions of GCM. 847 848 <p>To allow the application to use GCM, add this permission to the manifest: 849 850 <pre><uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /></pre> 851 852 <p>GCM delivers messages as a broadcast. The receivers must be registered in the manifest in 853 order to wake up the application. 854 855 <p>The <code>com.google.android.c2dm.permission.SEND permission</code> is held by Google Play 856 services. This prevents other code from invoking the broadcast receiver. Here is an excerpt 857 from a sample manifest: 858 859 <pre> 860 <receiver android:name=".MyReceiver" android:exported="true" 861 android:permission="com.google.android.c2dm.permission.SEND" > 862 <intent-filter> 863 <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 864 <category android:name="YOUR_PACKAGE_NAME" /> 865 </intent-filter> 866 </receiver></pre> 867 868 <p>When a GCM connection server delivers the message to your client app, the 869 <code>BroadcastReceiver</code> receives the message as an intent. You can either process the 870 intent in the <code>BroadcastReceiver</code>, or you can pass off the work of processing the 871 intent to a service (typically, an <code>IntentService</code>). If you use a service, your 872 broadcast receiver should be an instance of <code>WakefulBroadcastReceiver</code>, to hold a 873 wake lock while the service is doing its work. 874 875 <p>When processing the intent GCM passes into your app's broadcast receiver, you can determine 876 the message type by calling <code><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#getMessageType(android.content.Intent)">getMessageType(intent)</a></code>. For example: 877 878 <pre> 879 GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 880 String messageType = gcm.getMessageType(intent); 881 ... 882 // Filter messages based on message type. It is likely that GCM will be extended in the future 883 // with new message types, so just ignore message types you're not interested in, or that you 884 // don't recognize. 885 if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { 886 // It's an error. 887 } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { 888 // Deleted messages on the server. 889 } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { 890 // It's a regular GCM message, do some work. 891 } 892 </pre> 893 894 <p>If you are using the XMPP-based 895 <a href="http://developer.android.com/google/gcm/ccs.html">Cloud Connection Server</a>, your 896 client app can send upstream messages back to the server. For example: 897 898 <pre> 899 gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data); 900 </pre> 901 902 For a more details, see 903 <a href="http://developer.android.com/google/gcm/client.html">Implementing GCM Client</a>. 904</p> 905 906 907 908 909 910</div><!-- jd-descr --> 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927<div class="jd-descr"> 928 929 930<h2>Summary</h2> 931 932 933 934 935 936 937 938 939 940 941 942 943 944<!-- =========== ENUM CONSTANT SUMMARY =========== --> 945<table id="constants" class="jd-sumtable"><tr><th colspan="12">Constants</th></tr> 946 947 948 949 <tr class="alt-color api apilevel-" > 950 <td class="jd-typecol">String</td> 951 <td class="jd-linkcol"><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#ERROR_MAIN_THREAD">ERROR_MAIN_THREAD</a></td> 952 <td class="jd-descrcol" width="100%">The GCM <code><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#register(java.lang.String...)">register()</a></code> and <code><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#unregister()">unregister()</a></code> methods are 953 blocking.</td> 954 </tr> 955 956 957 <tr class=" api apilevel-" > 958 <td class="jd-typecol">String</td> 959 <td class="jd-linkcol"><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#ERROR_SERVICE_NOT_AVAILABLE">ERROR_SERVICE_NOT_AVAILABLE</a></td> 960 <td class="jd-descrcol" width="100%">The device can't read the response, or there was a 500/503 from the 961 server that can be retried later.</td> 962 </tr> 963 964 965 <tr class="alt-color api apilevel-" > 966 <td class="jd-typecol">String</td> 967 <td class="jd-linkcol"><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#MESSAGE_TYPE_DELETED">MESSAGE_TYPE_DELETED</a></td> 968 <td class="jd-descrcol" width="100%">Returned by <code><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#getMessageType(android.content.Intent)">getMessageType(Intent)</a></code> to indicate that the server deleted 969 some pending messages because they were collapsible.</td> 970 </tr> 971 972 973 <tr class=" api apilevel-" > 974 <td class="jd-typecol">String</td> 975 <td class="jd-linkcol"><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#MESSAGE_TYPE_MESSAGE">MESSAGE_TYPE_MESSAGE</a></td> 976 <td class="jd-descrcol" width="100%">Returned by <code><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#getMessageType(android.content.Intent)">getMessageType(Intent)</a></code> to indicate a regular message.</td> 977 </tr> 978 979 980 <tr class="alt-color api apilevel-" > 981 <td class="jd-typecol">String</td> 982 <td class="jd-linkcol"><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#MESSAGE_TYPE_SEND_ERROR">MESSAGE_TYPE_SEND_ERROR</a></td> 983 <td class="jd-descrcol" width="100%">Returned by <code><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#getMessageType(android.content.Intent)">getMessageType(Intent)</a></code> to indicate a send error.</td> 984 </tr> 985 986 987 988</table> 989 990 991 992 993 994 995 996 997 998 999 1000 1001<!-- ======== CONSTRUCTOR SUMMARY ======== --> 1002<table id="pubctors" class="jd-sumtable"><tr><th colspan="12">Public Constructors</th></tr> 1003 1004 1005 1006 1007 <tr class="alt-color api apilevel-" > 1008 <td class="jd-typecol"><nobr> 1009 1010 1011 1012 1013 1014 </nobr> 1015 </td> 1016 <td class="jd-linkcol" width="100%"><nobr> 1017 <span class="sympad"><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#GoogleCloudMessaging()">GoogleCloudMessaging</a></span>()</nobr> 1018 1019 </td></tr> 1020 1021 1022 1023</table> 1024 1025 1026 1027 1028 1029 1030<!-- ========== METHOD SUMMARY =========== --> 1031<table id="pubmethods" class="jd-sumtable"><tr><th colspan="12">Public Methods</th></tr> 1032 1033 1034 1035 1036 <tr class="alt-color api apilevel-" > 1037 <td class="jd-typecol"><nobr> 1038 1039 1040 1041 1042 1043 void</nobr> 1044 </td> 1045 <td class="jd-linkcol" width="100%"><nobr> 1046 <span class="sympad"><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#close()">close</a></span>()</nobr> 1047 1048 <div class="jd-descrdiv">Must be called when your application is done using GCM, to release 1049 internal resources.</div> 1050 1051 </td></tr> 1052 1053 1054 1055 <tr class=" api apilevel-" > 1056 <td class="jd-typecol"><nobr> 1057 1058 synchronized 1059 1060 static 1061 1062 <a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html">GoogleCloudMessaging</a></nobr> 1063 </td> 1064 <td class="jd-linkcol" width="100%"><nobr> 1065 <span class="sympad"><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#getInstance(android.content.Context)">getInstance</a></span>(Context context)</nobr> 1066 1067 <div class="jd-descrdiv">Return the singleton instance of GCM.</div> 1068 1069 </td></tr> 1070 1071 1072 1073 <tr class="alt-color api apilevel-" > 1074 <td class="jd-typecol"><nobr> 1075 1076 1077 1078 1079 1080 String</nobr> 1081 </td> 1082 <td class="jd-linkcol" width="100%"><nobr> 1083 <span class="sympad"><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#getMessageType(android.content.Intent)">getMessageType</a></span>(Intent intent)</nobr> 1084 1085 <div class="jd-descrdiv">Return the message type from an intent passed into a client app's broadcast receiver.</div> 1086 1087 </td></tr> 1088 1089 1090 1091 <tr class=" api apilevel-" > 1092 <td class="jd-typecol"><nobr> 1093 1094 1095 1096 1097 1098 String</nobr> 1099 </td> 1100 <td class="jd-linkcol" width="100%"><nobr> 1101 <span class="sympad"><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#register(java.lang.String...)">register</a></span>(String... senderIds)</nobr> 1102 1103 <div class="jd-descrdiv">Register the application for GCM and return the registration ID.</div> 1104 1105 </td></tr> 1106 1107 1108 1109 <tr class="alt-color api apilevel-" > 1110 <td class="jd-typecol"><nobr> 1111 1112 1113 1114 1115 1116 void</nobr> 1117 </td> 1118 <td class="jd-linkcol" width="100%"><nobr> 1119 <span class="sympad"><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#send(java.lang.String, java.lang.String, long, android.os.Bundle)">send</a></span>(String to, String msgId, long timeToLive, Bundle data)</nobr> 1120 1121 <div class="jd-descrdiv">Send an upstream ("device to cloud") message.</div> 1122 1123 </td></tr> 1124 1125 1126 1127 <tr class=" api apilevel-" > 1128 <td class="jd-typecol"><nobr> 1129 1130 1131 1132 1133 1134 void</nobr> 1135 </td> 1136 <td class="jd-linkcol" width="100%"><nobr> 1137 <span class="sympad"><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#send(java.lang.String, java.lang.String, android.os.Bundle)">send</a></span>(String to, String msgId, Bundle data)</nobr> 1138 1139 <div class="jd-descrdiv">Send an upstream ("device to cloud") message.</div> 1140 1141 </td></tr> 1142 1143 1144 1145 <tr class="alt-color api apilevel-" > 1146 <td class="jd-typecol"><nobr> 1147 1148 1149 1150 1151 1152 void</nobr> 1153 </td> 1154 <td class="jd-linkcol" width="100%"><nobr> 1155 <span class="sympad"><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#unregister()">unregister</a></span>()</nobr> 1156 1157 <div class="jd-descrdiv">Unregister the application.</div> 1158 1159 </td></tr> 1160 1161 1162 1163</table> 1164 1165 1166 1167 1168 1169 1170 1171<!-- ========== METHOD SUMMARY =========== --> 1172<table id="inhmethods" class="jd-sumtable"><tr><th> 1173 <a href="#" class="toggle-all" onclick="return toggleAllInherited(this, null)">[Expand]</a> 1174 <div style="clear:left;">Inherited Methods</div></th></tr> 1175 1176 1177<tr class="api apilevel-" > 1178<td colspan="12"> 1179 <a href="#" onclick="return toggleInherited(this, null)" id="inherited-methods-java.lang.Object" class="jd-expando-trigger closed" 1180 ><img id="inherited-methods-java.lang.Object-trigger" 1181 src="/assets/images/triangle-closed.png" 1182 class="jd-expando-trigger-img" /></a> 1183From class 1184 1185 java.lang.Object 1186 1187<div id="inherited-methods-java.lang.Object"> 1188 <div id="inherited-methods-java.lang.Object-list" 1189 class="jd-inheritedlinks"> 1190 </div> 1191 <div id="inherited-methods-java.lang.Object-summary" style="display: none;"> 1192 <table class="jd-sumtable-expando"> 1193 1194 1195 1196 1197 <tr class="alt-color api apilevel-" > 1198 <td class="jd-typecol"><nobr> 1199 1200 1201 1202 1203 1204 Object</nobr> 1205 </td> 1206 <td class="jd-linkcol" width="100%"><nobr> 1207 <span class="sympad">clone</span>()</nobr> 1208 1209 </td></tr> 1210 1211 1212 1213 <tr class=" api apilevel-" > 1214 <td class="jd-typecol"><nobr> 1215 1216 1217 1218 1219 1220 boolean</nobr> 1221 </td> 1222 <td class="jd-linkcol" width="100%"><nobr> 1223 <span class="sympad">equals</span>(Object arg0)</nobr> 1224 1225 </td></tr> 1226 1227 1228 1229 <tr class="alt-color api apilevel-" > 1230 <td class="jd-typecol"><nobr> 1231 1232 1233 1234 1235 1236 void</nobr> 1237 </td> 1238 <td class="jd-linkcol" width="100%"><nobr> 1239 <span class="sympad">finalize</span>()</nobr> 1240 1241 </td></tr> 1242 1243 1244 1245 <tr class=" api apilevel-" > 1246 <td class="jd-typecol"><nobr> 1247 1248 1249 final 1250 1251 1252 Class<?></nobr> 1253 </td> 1254 <td class="jd-linkcol" width="100%"><nobr> 1255 <span class="sympad">getClass</span>()</nobr> 1256 1257 </td></tr> 1258 1259 1260 1261 <tr class="alt-color api apilevel-" > 1262 <td class="jd-typecol"><nobr> 1263 1264 1265 1266 1267 1268 int</nobr> 1269 </td> 1270 <td class="jd-linkcol" width="100%"><nobr> 1271 <span class="sympad">hashCode</span>()</nobr> 1272 1273 </td></tr> 1274 1275 1276 1277 <tr class=" api apilevel-" > 1278 <td class="jd-typecol"><nobr> 1279 1280 1281 final 1282 1283 1284 void</nobr> 1285 </td> 1286 <td class="jd-linkcol" width="100%"><nobr> 1287 <span class="sympad">notify</span>()</nobr> 1288 1289 </td></tr> 1290 1291 1292 1293 <tr class="alt-color api apilevel-" > 1294 <td class="jd-typecol"><nobr> 1295 1296 1297 final 1298 1299 1300 void</nobr> 1301 </td> 1302 <td class="jd-linkcol" width="100%"><nobr> 1303 <span class="sympad">notifyAll</span>()</nobr> 1304 1305 </td></tr> 1306 1307 1308 1309 <tr class=" api apilevel-" > 1310 <td class="jd-typecol"><nobr> 1311 1312 1313 1314 1315 1316 String</nobr> 1317 </td> 1318 <td class="jd-linkcol" width="100%"><nobr> 1319 <span class="sympad">toString</span>()</nobr> 1320 1321 </td></tr> 1322 1323 1324 1325 <tr class="alt-color api apilevel-" > 1326 <td class="jd-typecol"><nobr> 1327 1328 1329 final 1330 1331 1332 void</nobr> 1333 </td> 1334 <td class="jd-linkcol" width="100%"><nobr> 1335 <span class="sympad">wait</span>()</nobr> 1336 1337 </td></tr> 1338 1339 1340 1341 <tr class=" api apilevel-" > 1342 <td class="jd-typecol"><nobr> 1343 1344 1345 final 1346 1347 1348 void</nobr> 1349 </td> 1350 <td class="jd-linkcol" width="100%"><nobr> 1351 <span class="sympad">wait</span>(long arg0, int arg1)</nobr> 1352 1353 </td></tr> 1354 1355 1356 1357 <tr class="alt-color api apilevel-" > 1358 <td class="jd-typecol"><nobr> 1359 1360 1361 final 1362 1363 1364 void</nobr> 1365 </td> 1366 <td class="jd-linkcol" width="100%"><nobr> 1367 <span class="sympad">wait</span>(long arg0)</nobr> 1368 1369 </td></tr> 1370 1371 1372</table> 1373 </div> 1374</div> 1375</td></tr> 1376 1377 1378</table> 1379 1380 1381</div><!-- jd-descr (summary) --> 1382 1383<!-- Details --> 1384 1385 1386 1387 1388 1389 1390 1391 1392<!-- XML Attributes --> 1393 1394 1395<!-- Enum Values --> 1396 1397 1398<!-- Constants --> 1399 1400 1401<!-- ========= ENUM CONSTANTS DETAIL ======== --> 1402<h2>Constants</h2> 1403 1404 1405 1406 1407<A NAME="ERROR_MAIN_THREAD"></A> 1408 1409<div class="jd-details api apilevel-"> 1410 <h4 class="jd-details-title"> 1411 <span class="normal"> 1412 public 1413 static 1414 final 1415 String 1416 </span> 1417 ERROR_MAIN_THREAD 1418 </h4> 1419 <div class="api-level"> 1420 1421 1422 1423 1424 </div> 1425 <div class="jd-details-descr"> 1426 1427 <div class="jd-tagdata jd-tagdescr"><p>The GCM <code><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#register(java.lang.String...)">register()</a></code> and <code><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#unregister()">unregister()</a></code> methods are 1428 blocking. You should not run them in the main thread or in broadcast receivers. 1429</p></div> 1430 1431 1432 <div class="jd-tagdata"> 1433 <span class="jd-tagtitle">Constant Value: </span> 1434 <span> 1435 1436 "MAIN_THREAD" 1437 1438 </span> 1439 </div> 1440 1441 </div> 1442</div> 1443 1444 1445 1446<A NAME="ERROR_SERVICE_NOT_AVAILABLE"></A> 1447 1448<div class="jd-details api apilevel-"> 1449 <h4 class="jd-details-title"> 1450 <span class="normal"> 1451 public 1452 static 1453 final 1454 String 1455 </span> 1456 ERROR_SERVICE_NOT_AVAILABLE 1457 </h4> 1458 <div class="api-level"> 1459 1460 1461 1462 1463 </div> 1464 <div class="jd-details-descr"> 1465 1466 <div class="jd-tagdata jd-tagdescr"><p>The device can't read the response, or there was a 500/503 from the 1467 server that can be retried later. The application should use exponential 1468 back off and retry. 1469</p></div> 1470 1471 1472 <div class="jd-tagdata"> 1473 <span class="jd-tagtitle">Constant Value: </span> 1474 <span> 1475 1476 "SERVICE_NOT_AVAILABLE" 1477 1478 </span> 1479 </div> 1480 1481 </div> 1482</div> 1483 1484 1485 1486<A NAME="MESSAGE_TYPE_DELETED"></A> 1487 1488<div class="jd-details api apilevel-"> 1489 <h4 class="jd-details-title"> 1490 <span class="normal"> 1491 public 1492 static 1493 final 1494 String 1495 </span> 1496 MESSAGE_TYPE_DELETED 1497 </h4> 1498 <div class="api-level"> 1499 1500 1501 1502 1503 </div> 1504 <div class="jd-details-descr"> 1505 1506 <div class="jd-tagdata jd-tagdescr"><p>Returned by <code><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#getMessageType(android.content.Intent)">getMessageType(Intent)</a></code> to indicate that the server deleted 1507 some pending messages because they were collapsible. 1508</p></div> 1509 1510 1511 <div class="jd-tagdata"> 1512 <span class="jd-tagtitle">Constant Value: </span> 1513 <span> 1514 1515 "deleted_messages" 1516 1517 </span> 1518 </div> 1519 1520 </div> 1521</div> 1522 1523 1524 1525<A NAME="MESSAGE_TYPE_MESSAGE"></A> 1526 1527<div class="jd-details api apilevel-"> 1528 <h4 class="jd-details-title"> 1529 <span class="normal"> 1530 public 1531 static 1532 final 1533 String 1534 </span> 1535 MESSAGE_TYPE_MESSAGE 1536 </h4> 1537 <div class="api-level"> 1538 1539 1540 1541 1542 </div> 1543 <div class="jd-details-descr"> 1544 1545 <div class="jd-tagdata jd-tagdescr"><p>Returned by <code><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#getMessageType(android.content.Intent)">getMessageType(Intent)</a></code> to indicate a regular message. 1546</p></div> 1547 1548 1549 <div class="jd-tagdata"> 1550 <span class="jd-tagtitle">Constant Value: </span> 1551 <span> 1552 1553 "gcm" 1554 1555 </span> 1556 </div> 1557 1558 </div> 1559</div> 1560 1561 1562 1563<A NAME="MESSAGE_TYPE_SEND_ERROR"></A> 1564 1565<div class="jd-details api apilevel-"> 1566 <h4 class="jd-details-title"> 1567 <span class="normal"> 1568 public 1569 static 1570 final 1571 String 1572 </span> 1573 MESSAGE_TYPE_SEND_ERROR 1574 </h4> 1575 <div class="api-level"> 1576 1577 1578 1579 1580 </div> 1581 <div class="jd-details-descr"> 1582 1583 <div class="jd-tagdata jd-tagdescr"><p>Returned by <code><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#getMessageType(android.content.Intent)">getMessageType(Intent)</a></code> to indicate a send error. 1584 The intent includes the message ID of the message and an error code. 1585</p></div> 1586 1587 1588 <div class="jd-tagdata"> 1589 <span class="jd-tagtitle">Constant Value: </span> 1590 <span> 1591 1592 "send_error" 1593 1594 </span> 1595 </div> 1596 1597 </div> 1598</div> 1599 1600 1601 1602 1603<!-- Fields --> 1604 1605 1606<!-- Public ctors --> 1607 1608 1609<!-- ========= CONSTRUCTOR DETAIL ======== --> 1610<h2>Public Constructors</h2> 1611 1612 1613 1614<A NAME="GoogleCloudMessaging()"></A> 1615 1616<div class="jd-details api apilevel-"> 1617 <h4 class="jd-details-title"> 1618 <span class="normal"> 1619 public 1620 1621 1622 1623 1624 1625 </span> 1626 <span class="sympad">GoogleCloudMessaging</span> 1627 <span class="normal">()</span> 1628 </h4> 1629 <div class="api-level"> 1630 <div></div> 1631 1632 1633 1634 </div> 1635 <div class="jd-details-descr"> 1636 1637 <div class="jd-tagdata jd-tagdescr"><p></p></div> 1638 1639 </div> 1640</div> 1641 1642 1643 1644 1645 1646<!-- ========= CONSTRUCTOR DETAIL ======== --> 1647<!-- Protected ctors --> 1648 1649 1650 1651<!-- ========= METHOD DETAIL ======== --> 1652<!-- Public methdos --> 1653 1654<h2>Public Methods</h2> 1655 1656 1657 1658<A NAME="close()"></A> 1659 1660<div class="jd-details api apilevel-"> 1661 <h4 class="jd-details-title"> 1662 <span class="normal"> 1663 public 1664 1665 1666 1667 1668 void 1669 </span> 1670 <span class="sympad">close</span> 1671 <span class="normal">()</span> 1672 </h4> 1673 <div class="api-level"> 1674 <div></div> 1675 1676 1677 1678 </div> 1679 <div class="jd-details-descr"> 1680 1681 <div class="jd-tagdata jd-tagdescr"><p>Must be called when your application is done using GCM, to release 1682 internal resources. 1683</p></div> 1684 1685 </div> 1686</div> 1687 1688 1689<A NAME="getInstance(android.content.Context)"></A> 1690 1691<div class="jd-details api apilevel-"> 1692 <h4 class="jd-details-title"> 1693 <span class="normal"> 1694 public 1695 static 1696 1697 1698 synchronized 1699 <a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html">GoogleCloudMessaging</a> 1700 </span> 1701 <span class="sympad">getInstance</span> 1702 <span class="normal">(Context context)</span> 1703 </h4> 1704 <div class="api-level"> 1705 <div></div> 1706 1707 1708 1709 </div> 1710 <div class="jd-details-descr"> 1711 1712 <div class="jd-tagdata jd-tagdescr"><p>Return the singleton instance of GCM.</p></div> 1713 1714 </div> 1715</div> 1716 1717 1718<A NAME="getMessageType(android.content.Intent)"></A> 1719 1720<div class="jd-details api apilevel-"> 1721 <h4 class="jd-details-title"> 1722 <span class="normal"> 1723 public 1724 1725 1726 1727 1728 String 1729 </span> 1730 <span class="sympad">getMessageType</span> 1731 <span class="normal">(Intent intent)</span> 1732 </h4> 1733 <div class="api-level"> 1734 <div></div> 1735 1736 1737 1738 </div> 1739 <div class="jd-details-descr"> 1740 1741 <div class="jd-tagdata jd-tagdescr"><p>Return the message type from an intent passed into a client app's broadcast receiver. There 1742 are two general categories of messages passed from the server: regular GCM messages, 1743 and special GCM status messages. 1744 1745 The possible types are: 1746 <ul> 1747 <li><code><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#MESSAGE_TYPE_MESSAGE">MESSAGE_TYPE_MESSAGE</a></code>—regular message from your server. 1748 </li><li><code><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#MESSAGE_TYPE_DELETED">MESSAGE_TYPE_DELETED</a></code>—special status message indicating that some 1749 messages have been collapsed by GCM. 1750 </li><li><code><a href="/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#MESSAGE_TYPE_SEND_ERROR">MESSAGE_TYPE_SEND_ERROR</a></code>—special status message indicating that 1751 there were errors sending one of the messages. 1752 </li></ul> 1753 1754 You can use this method to filter based on message type. Since it is likely that GCM will 1755 be extended in the future with new message types, just ignore any message types you're not 1756 interested in, or that you don't recognize.</p></div> 1757 <div class="jd-tagdata"> 1758 <h5 class="jd-tagtitle">Returns</h5> 1759 <ul class="nolist"><li>The message type or null if the intent is not a GCM intent 1760</li></ul> 1761 </div> 1762 1763 </div> 1764</div> 1765 1766 1767<A NAME="register(java.lang.String...)"></A> 1768 1769<div class="jd-details api apilevel-"> 1770 <h4 class="jd-details-title"> 1771 <span class="normal"> 1772 public 1773 1774 1775 1776 1777 String 1778 </span> 1779 <span class="sympad">register</span> 1780 <span class="normal">(String... senderIds)</span> 1781 </h4> 1782 <div class="api-level"> 1783 <div></div> 1784 1785 1786 1787 </div> 1788 <div class="jd-details-descr"> 1789 1790 <div class="jd-tagdata jd-tagdescr"><p>Register the application for GCM and return the registration ID. You must call this once, 1791 when your application is installed, and send the returned registration ID to the server. 1792 <p> 1793 Repeated calls to this method will return the original registration ID. 1794 <p> 1795 If you want to modify the list of senders, you must call <code>unregister()</code> first. 1796 <p> 1797 Most applications use a single sender ID. You may use multiple senders if different 1798 servers may send messages to the app or for testing.</p></div> 1799 <div class="jd-tagdata"> 1800 <h5 class="jd-tagtitle">Parameters</h5> 1801 <table class="jd-tagtable"> 1802 <tr> 1803 <th>senderIds</td> 1804 <td>list of project numbers or Google accounts identifying who is allowed to 1805 send messages to this application.</td> 1806 </tr> 1807 </table> 1808 </div> 1809 <div class="jd-tagdata"> 1810 <h5 class="jd-tagtitle">Returns</h5> 1811 <ul class="nolist"><li>registration id 1812</li></ul> 1813 </div> 1814 <div class="jd-tagdata"> 1815 <h5 class="jd-tagtitle">Throws</h5> 1816 <table class="jd-tagtable"> 1817 <tr> 1818 <th>IOException</td> 1819 <td></td> 1820 </tr> 1821 </table> 1822 </div> 1823 1824 </div> 1825</div> 1826 1827 1828<A NAME="send(java.lang.String, java.lang.String, long, android.os.Bundle)"></A> 1829 1830<div class="jd-details api apilevel-"> 1831 <h4 class="jd-details-title"> 1832 <span class="normal"> 1833 public 1834 1835 1836 1837 1838 void 1839 </span> 1840 <span class="sympad">send</span> 1841 <span class="normal">(String to, String msgId, long timeToLive, Bundle data)</span> 1842 </h4> 1843 <div class="api-level"> 1844 <div></div> 1845 1846 1847 1848 </div> 1849 <div class="jd-details-descr"> 1850 1851 <div class="jd-tagdata jd-tagdescr"><p>Send an upstream ("device to cloud") message. You can only use the upstream feature 1852 if your GCM implementation uses the XMPP-based 1853 <a href="http://developer.android.com/google/gcm/ccs.html">Cloud Connection Server</a>. 1854 1855 The current limits for max storage time and number of outstanding messages per 1856 application are documented in the 1857 <a href="http://developer.android.com/google/gcm/index.html">GCM Developers Guide</a>.</p></div> 1858 <div class="jd-tagdata"> 1859 <h5 class="jd-tagtitle">Parameters</h5> 1860 <table class="jd-tagtable"> 1861 <tr> 1862 <th>to</td> 1863 <td>string identifying the receiver of the message. For GCM projects IDs 1864 the value is <code>SENDER_ID@gcm.googleapis.com</code>. The <code>SENDER_ID</code> 1865 should be one of the sender IDs used in <code>register()</code>.</td> 1866 </tr> 1867 <tr> 1868 <th>msgId</td> 1869 <td>ID of the message. This is generated by the application. It must be 1870 unique for each message. This allows error callbacks and debugging.</td> 1871 </tr> 1872 <tr> 1873 <th>timeToLive</td> 1874 <td>If 0, we'll attempt to send immediately and return an 1875 error if we're not connected. Otherwise, the message will be queued. 1876 As for server-side messages, we don't return an error if the message has been 1877 dropped because of TTL—this can happen on the server side, and it would require 1878 extra communication.</td> 1879 </tr> 1880 <tr> 1881 <th>data</td> 1882 <td>key/value pairs to be sent. Values must be String, any other type will 1883 be ignored.</td> 1884 </tr> 1885 </table> 1886 </div> 1887 <div class="jd-tagdata"> 1888 <h5 class="jd-tagtitle">Throws</h5> 1889 <table class="jd-tagtable"> 1890 <tr> 1891 <th>IllegalArgumentException</td> 1892 <td></td> 1893 </tr> 1894 <tr> 1895 <th>IOException</td> 1896 <td></td> 1897 </tr> 1898 </table> 1899 </div> 1900 1901 </div> 1902</div> 1903 1904 1905<A NAME="send(java.lang.String, java.lang.String, android.os.Bundle)"></A> 1906 1907<div class="jd-details api apilevel-"> 1908 <h4 class="jd-details-title"> 1909 <span class="normal"> 1910 public 1911 1912 1913 1914 1915 void 1916 </span> 1917 <span class="sympad">send</span> 1918 <span class="normal">(String to, String msgId, Bundle data)</span> 1919 </h4> 1920 <div class="api-level"> 1921 <div></div> 1922 1923 1924 1925 </div> 1926 <div class="jd-details-descr"> 1927 1928 <div class="jd-tagdata jd-tagdescr"><p>Send an upstream ("device to cloud") message. You can only use the upstream feature 1929 if your GCM implementation uses the XMPP-based 1930 <a href="http://developer.android.com/google/gcm/ccs.html">Cloud Connection Server</a>. 1931 1932 The message will be queued if we don't have an active connection for the max interval.</p></div> 1933 <div class="jd-tagdata"> 1934 <h5 class="jd-tagtitle">Parameters</h5> 1935 <table class="jd-tagtable"> 1936 <tr> 1937 <th>to</td> 1938 <td>string identifying the receiver of the message. For GCM project IDs 1939 the value is <code>SENDER_ID@gcm.googleapis.com</code>. The <code>SENDER_ID</code> 1940 should be one of the sender IDs used in <code>register()</code>.</td> 1941 </tr> 1942 <tr> 1943 <th>msgId</td> 1944 <td>ID of the message. This is generated by the application. It must be 1945 unique for each message. This allows error callbacks and debugging.</td> 1946 </tr> 1947 <tr> 1948 <th>data</td> 1949 <td>key/value pairs to be sent. Values must be String—any other type will 1950 be ignored.</td> 1951 </tr> 1952 </table> 1953 </div> 1954 <div class="jd-tagdata"> 1955 <h5 class="jd-tagtitle">Throws</h5> 1956 <table class="jd-tagtable"> 1957 <tr> 1958 <th></td> 1959 <td>IllegalArgumentException</td> 1960 </tr> 1961 <tr> 1962 <th>IOException</td> 1963 <td></td> 1964 </tr> 1965 </table> 1966 </div> 1967 1968 </div> 1969</div> 1970 1971 1972<A NAME="unregister()"></A> 1973 1974<div class="jd-details api apilevel-"> 1975 <h4 class="jd-details-title"> 1976 <span class="normal"> 1977 public 1978 1979 1980 1981 1982 void 1983 </span> 1984 <span class="sympad">unregister</span> 1985 <span class="normal">()</span> 1986 </h4> 1987 <div class="api-level"> 1988 <div></div> 1989 1990 1991 1992 </div> 1993 <div class="jd-details-descr"> 1994 1995 <div class="jd-tagdata jd-tagdescr"><p>Unregister the application. Calling <code>unregister()</code> stops any 1996 messages from the server. This is a blocking call—you shouldn't call 1997 it from the UI thread. 1998 1999 You should rarely (if ever) need to call this method. Not only is it 2000 expensive in terms of resources, but it invalidates your registration ID, 2001 which you should never change unnecessarily. A better approach is to simply 2002 have your server stop sending messages. Only use unregister if you want 2003 to change your sender ID.</p></div> 2004 <div class="jd-tagdata"> 2005 <h5 class="jd-tagtitle">Throws</h5> 2006 <table class="jd-tagtable"> 2007 <tr> 2008 <th>IOException</td> 2009 <td>if we can't connect to server to unregister. 2010</td> 2011 </tr> 2012 </table> 2013 </div> 2014 2015 </div> 2016</div> 2017 2018 2019 2020 2021 2022<!-- ========= METHOD DETAIL ======== --> 2023 2024 2025 2026<!-- ========= END OF CLASS DATA ========= --> 2027<A NAME="navbar_top"></A> 2028 2029<div id="footer" class="wrap" > 2030 2031 2032 <div id="copyright"> 2033 2034 Except as noted, this content is licensed under <a 2035 href="http://www.apache.org/licenses/LICENSE-2.0">Apache 2.0</a>. 2036 For details and restrictions, see the <a href="/license.html"> 2037 Content License</a>. 2038 </div> 2039 <div id="build_info"> 2040 2041<script src="/timestamp.js" type="text/javascript"></script> 2042<script>document.write(BUILD_TIMESTAMP)</script> 2043 2044 </div> 2045 2046 2047 <div id="footerlinks"> 2048 2049 <p> 2050 <a href="/about/index.html">About Android</a> | 2051 <a href="/legal.html">Legal</a> | 2052 <a href="/support.html">Support</a> 2053 </p> 2054 </div> 2055 2056</div> <!-- end footer --> 2057</div> <!-- jd-content --> 2058 2059</div><!-- end doc-content --> 2060 2061</div> <!-- end body-content --> 2062 2063 2064 2065 2066 2067 2068</body> 2069</html> 2070