6

मैं पुश सूचनाओं के लिए Google क्लाउड मैसेजिंग (जीसीएम) सेवा का उपयोग करने के लिए अपने पहले एंड्रॉइड ऐप पर काम कर रहा हूं। मुझे उस बिंदु पर जाना है जहां मैं अपने सर्वर एप्लिकेशन से सफलतापूर्वक एक संदेश भेज सकता हूं, और क्लाइंट ऐप पर मेरी GCMIntentService क्लास में ऑन मैसेज इवेंट में संदेश की सामग्री लॉग कर सकता हूं। हालांकि मुझे डिवाइस पर कोई दृश्य संकेत नहीं दिख रहा है कि एक संदेश प्राप्त हुआ था। मैं फोन पर पुल-डाउन नोटिफिकेशन सूची में संदेश प्रकट होने की उम्मीद कर रहा था, क्योंकि यह आईफोन पर करता है। क्या इसे मैन्युअल रूप से कोड किया जाना है? संदेश को प्रदर्शित करने के लिए भी एक आम विधि है, इस पर ध्यान दिए बिना कि कौन सी गतिविधि वर्तमान में सक्रिय है, और यदि ऐप पृष्ठभूमि में निष्क्रिय है? किसी भी मदद की सराहना की।एंड्रॉइड - जीसीएम पुश सूचनाएं अधिसूचना सूची में दिखाई नहीं दे रही हैं

उत्तर

7

यह कोड स्क्रीन के शीर्ष पर एंड्रॉइड सिस्टम बार में एक अधिसूचना उत्पन्न करेगा। यह कोड एक नया इरादा बनाएगा जो उपयोगकर्ता को शीर्ष बार में अधिसूचना पर क्लिक करने के बाद "होम.क्लास" पर निर्देशित करेगा। यदि आप वर्तमान गतिविधि के आधार पर कुछ विशिष्ट करना चाहते हैं तो आप GCMIntentService से अपनी अन्य गतिविधियों में प्रसारण अनुरोध भेज सकते हैं।

Intent notificationIntent=new Intent(context, Home.class); 
generateNotification(context, message, notificationIntent); 

private static void generateNotification(Context context, String message, Intent notificationIntent) { 
    int icon = R.drawable.icon; 
    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) 
      context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 
    String title = context.getString(R.string.app_name); 

    // set intent so it does not start a new activity 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
      Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notificationManager.notify(0, notification); 
} 

ध्यान दें कि यह उदाहरण R.drawable और R.String में संसाधन है कि काम करने के लिए उपस्थित होने की आवश्यकता होगी का उपयोग करता है, लेकिन यह आप से पता चलना चाहिए। स्टेटस नोटिफिकेशन http://developer.android.com/guide/topics/ui/notifiers/index.html और प्रसारण रिसीवर के बारे में अधिक जानकारी के लिए इसे देखें। http://developer.android.com/reference/android/content/BroadcastReceiver.html

+0

शानदार, बहुत धन्यवाद है कि में जोड़ें। –

+0

हाय, मैंने इस प्लगइन का उपयोग https://github.com/marknutter/GCM-Cordova किया है और सभी निर्देशों का पालन किया है। हालांकि, ऐप के भीतर ही मुझे संदेश मिल सकते हैं, लेकिन ऐप को एंड्रॉइड अधिसूचना क्षेत्र में नोटिफिकेशन प्रदर्शित करने के लिए नहीं मिल सकता है। कृपया सलाह दें। मैं इस प्लगइन का उपयोग करता हूं क्योंकि मैं फोनगैप @ ज़ैचरी मोसांस्की –

+0

का उपयोग करता हूं, मुझे खेद है, लेकिन मैंने फोनगैप का कभी भी उपयोग नहीं किया है और इस पर टिप्पणी करने में असमर्थ हूं कि अधिसूचनाएं इसके साथ काम कैसे करें –

1

आप GcmListenerService उपयोग कर रहे हैं तो आप इस कोड का उपयोग करके अपने onMessageReceived sendNotification()

@Override 
public void onMessageReceived(String from, Bundle data) { 
     String message = data.getString("message"); 
     sendNotification(message); 
} 

private void sendNotification(String message) { 
     Intent intent = new Intent(this, YOURCLASS.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
     PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_park_notification) 
       .setContentTitle("Ppillo Message") 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } 
संबंधित मुद्दे