2012-09-24 16 views
13

मैंने इस ट्यूटोरियल से Android में पुश अधिसूचना एप्लिकेशन विकसित किया है: push notification in android app। जब मैं ऐप चलाता हूं तो रजिस्टर बटन प्रदर्शित होता है। जब मैं रजिस्टर बटन पर क्लिक करता हूं, और जब पंजीकरण सफल होता है, तो मेरे डिवाइस पर एक सूचना प्रदर्शित होती है।अपने एंड्रॉइड ऐप में पुश अधिसूचना कैसे जोड़ें

मैं इसे अपने ऐप में कैसे शामिल कर सकता हूं? मेरे ऐप में एक एक्सएमएल पार्सिंग उदाहरण ऐप है। यहां जब कोई नया आइटम जोड़ा जाता है, तो मैं डिवाइस पर एक अधिसूचना संदेश प्रदर्शित करना चाहता हूं (नया ऑर्डर प्रदर्शित होता है)। यह स्वचालित रूप से यहां जेनरेट किया जाता है।

+2

C2DM अब मान्य नहीं है का पालन करें। कृपया https://developer.android.com/guide/google/gcm/index.html – gigadot

+0

का उपयोग करें ठीक है, मैं उपरोक्त ट्यूटोरियल – user1676640

+1

यहां मेरे उत्तर को देखने के लिए सीखने और विकसित करने का प्रयास करूंगा: उम्मीद है कि यह मदद करता है: http: // stackoverflow। कॉम/ए/12437549/554740 – HelmiB

उत्तर

17

मैं Google Cloud Messaging का डेमो एप्लिकेशन पोस्ट कर रहा हूं।

आप इस सेवा का उपयोग करने के लिए कम से कम एक में हस्ताक्षर किए गए Google खाते के साथ एपीआई स्तर बराबर या

एंड्रॉयड गूगल एपीआई के साथ ओएस 2.2 की तुलना में अधिक उपयोगकर्ता डेमो आवेदन है बनाना सुनिश्चित करें।

सबसे पहले आपको GCM library जोड़ना होगा।

से वर्ग जो मैं GCMIntentService नामित जो GCMBaseIntentService फैली इस प्रकार पर बनाने के लिए:

package com.example.gcmdemo; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 

import com.google.android.gcm.GCMRegistrar; 

public class MainActivity extends Activity { 

    private static final String TAG = "Push Notification Demo Activity"; 
    private static final String SENDER_ID = "1069713227710"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     GCMRegistrar.checkDevice(this); 
     GCMRegistrar.checkManifest(this); 
     final String regId = GCMRegistrar.getRegistrationId(this); 
     if (regId.equals("")) { 
      GCMRegistrar.register(this, SENDER_ID); 
     } else { 
      Log.v(TAG, "Already registered : "+regId); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
} 

और अंत में डेमो प्रकट:

package com.example.gcmdemo; 

import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 

import com.google.android.gcm.GCMBaseIntentService; 
import com.google.android.gcm.GCMConstants; 

public class GCMIntentService extends GCMBaseIntentService { 

    private static final String TAG = "Push Notification Demo GCMIntentService"; 

    @Override 
    protected void onError(Context context, String errorId) { 

     if(GCMConstants.ERROR_ACCOUNT_MISSING.equalsIgnoreCase(errorId)) { 
      Log.v(TAG, "Error Account Missing"); 
     } else if(GCMConstants.ERROR_AUTHENTICATION_FAILED.equalsIgnoreCase(errorId)) { 
      Log.v(TAG, "Error Authentication Failed"); 
     } else if(GCMConstants.ERROR_INVALID_PARAMETERS.equalsIgnoreCase(errorId)) { 
      Log.v(TAG, "Error Invalid Parameters"); 
     } else if(GCMConstants.ERROR_INVALID_SENDER.equalsIgnoreCase(errorId)) { 
      Log.v(TAG, "Error Invalid Sender"); 
     } else if(GCMConstants.ERROR_PHONE_REGISTRATION_ERROR.equalsIgnoreCase(errorId)) { 
      Log.v(TAG, "Error Phone Registration Error"); 
     } else if(GCMConstants.ERROR_SERVICE_NOT_AVAILABLE.equalsIgnoreCase(errorId)) { 
      Log.v(TAG, "Error Service Not Available"); 
     } 
    } 

    @Override 
    protected void onMessage(Context context, Intent intent) { 

     // App Server Sends message as key value pairs 
     String value1 = intent.getStringExtra("key1"); 
     String value2 = intent.getStringExtra("key2"); 

     Log.v(TAG, "key1: "+value1); 
     Log.v(TAG, "key2: "+value2); 
    } 

    @Override 
    protected void onRegistered(Context context, String regId) { 

     Log.v(TAG, "Successfull Registration : "+regId); 
    } 

    @Override 
    protected void onUnregistered(Context context, String regId) { 

     Log.v(TAG, "Successfully Unregistred : "+regId); 
    } 

    @Override 
    protected String[] getSenderIds(Context context) { 
     return super.getSenderIds(context); 
    } 

    @Override 
    protected void onDeletedMessages(Context context, int total) { 
     super.onDeletedMessages(context, total); 
    } 

    @Override 
    protected boolean onRecoverableError(Context context, String errorId) { 
     return super.onRecoverableError(context, errorId); 
    } 
} 

का तरीका यहां बताया डेमो गतिविधि के बाद में पंजीकरण की जाँच करनी चाहिए है:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.gcmdemo" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="8" /> 

    <permission 
     android:name="com.example.gcmdemo.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="com.example.gcmdemo.permission.C2D_MESSAGE" /> 

    <!-- App receives GCM messages. --> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <!-- GCM connects to Google Services. --> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <!-- GCM requires a Google account. --> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <!-- Keeps the processor from sleeping when a message is received. --> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/title_activity_main" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver 
      android:name="com.google.android.gcm.GCMBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 

       <category android:name="com.example.gcmdemo" /> 
      </intent-filter> 
     </receiver> 

     <service android:name=".GCMIntentService" /> 
    </application> 

</manifest> 

इसके अलावा आप एन ईडी third party server side script as specified here

+0

या मैंने विधियों के ऊपर किया है। ऐप सफलतापूर्वक चलाया गया है और listview.i को सफलतापूर्वक प्रदर्शित किया गया है, मुझे अपने डेटाबेस पर सफलतापूर्वक एक आइटम डालना होगा, मैं अपने डिवाइस पर कोई सूचना संदेश प्रदर्शित नहीं करूँगा। – user1676640

+0

जीसीएम में पुश नोटिस की मात्रा सीमित करें? –

2

व्यक्तिगत रूप से आप का सुझाव भी है कि GCM के बजाय अन्य PushNotification के लिए Parse नामित पुस्तकालय, यह Google क्लाउड संदेश सेवा के रूप में ही काम करता है लेकिन यह इतना इतना तो इतना आसान तो GCM

तुम बस JAR फ़ाइल डाउनलोड करने के लिए है और पुश-सूचना के लिए कोड के सरल दो-तीन लाइन

इस साइट https://parse.com/tutorials/android-push-notifications

यहां तक ​​कि आप का उपयोग नहीं है PHP या सर्वर साइड कोड के किसी भी प्रकार का उपयोग करने के जानने के लिए यह आप की सुविधा प्रदान

012,

देखो मैं यू डेमो

Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY"); 
    PushService.setDefaultPushCallback(this, YourDefaultActivity.class); 

कोड ऊपर से दे देंगे पुश अधिसूचना

प्राप्त करने के लिए पर्याप्त है अगर आप भेजने अधिसूचना करना चाहते हैं वे अच्छे यूआई प्रदान यूआई की तस्वीर वे

प्रदान देखो

enter image description here

+1

पार्स वाणिज्यिक प्रस्ताव है। क्या जीसीएम के पास एक निश्चित सीमा के बाद समान प्रतिबंध (मूल्य) है? –

+0

पार्स सास मॉडल में केवल वाणिज्यिक है। उनके पास github पर उनके सर्वर का ओपन सोर्स संस्करण भी है – kacper3w

2

FCM

का उपयोग कर पुश अधिसूचना भेजा जा रहा है गूगल गूगल क्लाउड मैसेजिंग (GCM) पदावनत और नए पुश अधिसूचना सर्वर Firebase क्लाउड संदेश सेवा (FCM) है कि शुभारंभ किया। FCM GCM की तरह ही है, FCM भी मोबाइल प्लेटफार्मों के लिए एक पार मंच मैसेजिंग समाधान है

Firebase क्लाउड संदेश तीन प्रकार के संदेश (Message types)

1.Notification संदेश

भेज सकते हैं दोनों अधिसूचना और डाटा

0,123,516 के साथ 2.Data संदेश

3.message

Firebase क्लाउड संदेश घालमेल कदम: -

1.SetUp नया प्रोजेक्ट या Firbase कंसोल में आयात परियोजना (https://firebase.google.com/)

2.Add के समान पैकेज नाम फायरबेस ऐप में ऐप।

3. "google-services.json" फ़ाइल प्राप्त करें और उस फ़ाइल को अपने प्रोजेक्ट के ऐप फ़ोल्डर में रखें। इस फ़ाइल में Google सेवा के लिए सभी यूआरएल और कुंजी शामिल हैं, इसलिए इस फ़ाइल को बदलें या संपादित न करें।

4. फायरबेस के लिए परियोजना में नई ग्रैडल निर्भरता जोड़ें।

//app/build.gradle 
dependencies { 
    compile 'com.google.firebase:firebase-messaging:9.6.0' 
} 

apply plugin: 'com.google.gms.google-services' 

5. एक कक्षा बनाएं जिसमें सभी निरंतर मूल्य शामिल हैं जिन्हें हम एफसीएम के लिए ऐप में उपयोग करते हैं।

public class Config { 
public static final String TOPIC_GLOBAL = "global"; 
// broadcast receiver intent filters 
public static final String REGISTRATION_COMPLETE = "registrationComplete"; 
public static final String PUSH_NOTIFICATION = "pushNotification"; 

// id to handle the notification in the notification tray 
public static final int NOTIFICATION_ID = 100; 
public static final int NOTIFICATION_ID_BIG_IMAGE = 101; 
public static final String SHARED_PREF = "ah_firebase"; 
} 

6. एक वर्ग नामित MyFirebaseInstanceIDService.java जो firebase पंजीकरण आईडी जो प्रत्येक अनुप्रयोग के लिए अद्वितीय हो जाएगा प्राप्त करता होगा बनाएँ। पंजीकरण आईडी का उपयोग एक ही डिवाइस को संदेश भेजने के लिए किया जाता है।

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 
    private static final String TAG = MyFirebaseInstanceIDService.class.getSimpleName(); 

    @Override 
    public void onTokenRefresh() { 
     super.onTokenRefresh(); 
     String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 

     // Saving reg id to shared preferences 
     storeRegIdInPref(refreshedToken); 

     // sending reg id to your server 
     sendRegistrationToServer(refreshedToken); 

     // Notify UI that registration has completed, so the progress indicator can be hidden. 
     Intent registrationComplete = new Intent(Config.REGISTRATION_COMPLETE); 
     registrationComplete.putExtra("token", refreshedToken); 
     LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete); 
    } 

    private void sendRegistrationToServer(final String token) { 
     // sending gcm token to server 
     Log.e(TAG, "sendRegistrationToServer: " + token); 
    } 

    private void storeRegIdInPref(String token) { 
    SharedPreferences pref =  getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0); 
     SharedPreferences.Editor editor = pref.edit(); 
     editor.putString("regId", token); 
     editor.commit(); 
    } 
    } 

7. MyFirebaseMessagingService.java नामक एक और सेवा वर्ग बनाएं। यह फायरबेस संदेश प्राप्त होगा।

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = MyFirebaseMessagingService.class.getSimpleName(); 

    private NotificationUtils notificationUtils; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Log.e(TAG, "From: " + remoteMessage.getFrom()); 

     if (remoteMessage == null) 
      return; 

     // Check if message contains a notification payload. 
     if (remoteMessage.getNotification() != null) { 
      Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody()); 
      handleNotification(remoteMessage.getNotification().getBody()); 
     } 
    } 
private void handleNotification(String message) { 
     if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { 
      // app is in foreground, broadcast the push message 
      Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); 
      pushNotification.putExtra("message", message); 
      LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); 

      // play notification sound 
      NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext()); 
      notificationUtils.playNotificationSound(); 
     }else{ 
      // If the app is in background, firebase itself handles the notification 
     } 
    } 
/** 
    * Showing notification with text only 
    */ 
    private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) { 
     notificationUtils = new NotificationUtils(context); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     notificationUtils.showNotificationMessage(title, message, timeStamp, intent); 
    } 

    /** 
    * Showing notification with text and image 
    */ 
    private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) { 
     notificationUtils = new NotificationUtils(context); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl); 
    } 
} 

AndroidManifest.xml 8.In इन दो firebase सेवाओं MyFirebaseMessagingService और MyFirebaseInstanceIDService जोड़ें।

<!-- Firebase Notifications --> 
     <service android:name=".service.MyFirebaseMessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
      </intent-filter> 
     </service> 

     <service android:name=".service.MyFirebaseInstanceIDService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
      </intent-filter> 
     </service> 
     <!-- ./Firebase Notifications --> 

अब बस Send your First Message

नोट्स:

* 1.Read के लिए Firebase Cloud Messaging *

Google डॉक 2. यदि आप एक स्थानांतरित करना चाहते हैं एंड्रॉइड के लिए एंड्रॉइड के लिए जीसीएम क्लाइंट ऐप eBase क्लाउड संदेश निम्न चरणों का और डॉक्टर (Migrate a GCM Client App)

3.Android नमूना ट्यूटोरियल और कोड (Receive Reengagement Notifications)

संबंधित मुद्दे