2016-06-03 6 views
5

इस नोब सवाल के लिए खेद है, मैं एंड्रॉइड विकास के लिए नया हूँ। मैं वर्तमान में एक प्रोजेक्ट पर काम कर रहा हूं जिसके लिए एक एंड्रॉइड डिवाइस पर पुश अधिसूचना भेजने की आवश्यकता है जहां मेरा ऐप इंस्टॉल है। मैंने फ़ायरबेस द्वारा त्वरित स्टार्ट ट्यूटोरियल का पालन करके इसे पहले ही किया है और मुझे अपने डिवाइस पर अधिसूचना सफलतापूर्वक प्राप्त हुई है।फायरबेस पुश अधिसूचना से डेटा कैसे प्राप्त करें और इसे अपने एंड्रॉइड गतिविधि में प्रदर्शित करें?

प्रश्न: मैं सर्वर द्वारा भेजे गए संदेश को कैसे पुनर्प्राप्त कर सकता हूं और उस संदेश को मेरी एंड्रॉइड गतिविधि में प्रदर्शित कर सकता हूं? अग्रिम में धन्यवाद।

मेरा कोड है। MainActivity.java

public class MainActivity extends AppCompatActivity { 
TextView textView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    TextView tv = (TextView) findViewById(R.id.tv); 

    /* 
    * Get the data from push notification and display it in TextView 
    * */ 

    tv.setText("Message retrieve from Push Notification"); 

} 

और यह मेरा MyFirebaseMessagingService.java

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    sendNotification(remoteMessage.getNotification().getBody()); 
} 

//This method is only generating push notification 
//It is same as we did in earlier posts 
public void sendNotification(String messageBody) { 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
      PendingIntent.FLAG_ONE_SHOT); 
    Intent notificationIntent = new Intent(this, MainActivity.class); 
    notificationIntent.putExtra("message", messageBody); 
    // set intent so it does not start a new activity 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
      Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle("Peoplelink Push Notification") 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

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

    notificationManager.notify(0, notificationBuilder.build()); 
} 
+0

हाय में, किसी को भी मुझे tihs साथ कर सकते हैं। – Daryll

उत्तर

1

है आपको कुछ अतिरिक्त संदेश (अन्य की तुलना में एक अधिसूचना पर पता चला है), तो आप सेट कर सकते हैं भेजना चाहते हैं के तहत फ़ील्ड में संदेश उन्नत विकल्प -> फायरबेस कंसोल पर कस्टम डेटा

उदाहरण के लिए, मान के रूप में कुंजी और संदेश मूल्य रूप संदेश डाल दिया।

के बाद आप इसके सामने, डेटा डिवाइस के लिए भेजा जाएगा और इरादे एक्स्ट्रा कलाकार के माध्यम से, गतिविधि है कि आप अधिसूचना इरादे में स्थापित करने के लिए पारित हो जाएगा।

इस प्रकार, डेटा प्राप्त करने, गतिविधि onCreate पर इस कोड जोड़ें:

Bundle extras = getIntent().getExtras(); 
if (extras != null) { 
    tv.setText(extras.getString("message", "empty message")); 
} 

संदेश अगर यह ठीक से सेट से पता चला की जाएगी।

2

यदि आप अपनी एंड्रॉइड गतिविधि के लिए एक फायरबेस मैसेजिंग सेवा प्रदान करना चाहते हैं। आप EventBus का उपयोग कर सकते हैं। एक एंड्रॉइड अनुकूलित घटना बस क्यू क्रियाकलाप, टुकड़े, थ्रेड, सेवाओं, आदि के बीच संचार को सरल बनाता है कम कोड, बेहतर गुणवत्ता।

अपने build.gradle फाइल करने के लिए EventBus जोड़े

compile 'org.greenrobot:eventbus:3.0.0' 

रजिस्टर अपने MainActivity.java और onMessageEvent विधि पैदा करते हैं।

public class MainActivity extends AppCompatActivity { 

TextView textView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    textView = (TextView) findViewById(R.id.tv); 

    EventBus.getDefault().register(this); 
} 

@Subscribe(threadMode = ThreadMode.MAIN) 
public void onMessageEvent(String message) { 
    textView.setText(message); 
}; 

और का उपयोग EventBus.getDefault().post() अपने MyFirebaseMessagingService.java

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    EventBus.getDefault().post(remoteMessage.getNotification().getBody()); 
} 
+0

यदि आपके द्वारा कभी भी लिंक प्रदान किए जाने वाले लिंक में अपनी पोस्ट में कुछ और विवरण जोड़ें। – dckuehn

+0

@dckuehn टिप के लिए धन्यवाद –

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