2017-05-03 118 views
0

我想發送和接收android通知。很多示例使用php和gcm。我嘗試這些例子,但是隻有當我在索引文件中輸入消息並單擊發送按鈕 (enter image description here)時纔會發送通知。我想發送通知我的手機其他手機使用相同的應用程序。我怎樣才能做到這一點 ?發送通知電話到手機

+0

我認爲這個鏈接將幫助你https://www.simplifiedcoding.net/firebase-cloud-messaging-android/ –

+0

http://stackoverflow.com/questions/38432243/how-to-send-device-to-device-通知使用fcm不使用xmpp或任何 –

+0

@AKSHAYMANAGOOLI我試過這個鏈接,但我得到運行時錯誤:**「.._應用程序V/FA:不活動,從服務斷開連接。** –

回答

0

我找到了解決這個問題的解決方案。我希望這也能爲你工作。

MyFirebaseIdClass:請看here。這個類將令牌ID和消息發送到gcm服務器。

MyFirebaseMessagingService類:

public class MyFirebaseMessagingService extends FirebaseMessagingService { 


private static final String TAG = "MyFirebaseMsgService"; 

/** Called when message is received. 
@param remoteMessage the message received from Firebase Cloud Messaging. 
*/ 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 

    // There are two types of messages data messages and notification messages. 
    // Data messages are handled here in onMessageReceived whether the app is in the foreground or background. Data messages are the type traditionally used with FCM. Notification messages are only received here in onMessageReceived when the app is in the foreground. When the app is in the background an automatically generated notification is displayed.When the user taps on the notification they are returned to the app. Messages containing both notification and data payloads are treated as notification messages. 

    Log.d(TAG, "From: " + remoteMessage.getFrom()); 

    // Check if message contains a notification payload. 
    if (remoteMessage.getNotification() != null) 
    { 
     Log.d(TAG, "Message Notification Body: "+remoteMessage.getData().get("title")); 
    } 

    //Display notification in notification bar 
    sendNotificationToBar(remoteMessage.getData().get("title")); 
} 


/**Create and show a simple notification containing the received push Notification. 
*@param messageBody FCM message body received. 
*/ 
private void sendNotificationToBar(String messageBody) { 
    Intent intent = new Intent(this,ChatActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    Uri defaultSoundUri= 
      RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle("FCM Push Notification") 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 

} 

}

請該行添加到您的清單文件:

<!-- [START firebase_service] --> 
    <service 
     android:name=".MyFirebaseMessagingService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
     </intent-filter> 
    </service> 
    <!-- [END firebase_service] --> 
    <!-- [START firebase_iid_service] --> 
    <service 
     android:name=".MyFirebaseInstanceIdService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
     </intent-filter> 
    </service><!-- ATTENTION: This was auto-generated to add Google Play services to your project for 
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. --> 
    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
    <!-- [END firebase_iid_service] --> 

終於在構建gradle這個app文件:

compile 'com.google.firebase:firebase-messaging:10.2.6' 
compile 'com.google.firebase:firebase-auth:10.2.6' 
compile 'com.android.volley:volley:1.0.0' 
compile 'com.loopj.android:android-async-http:1.4.9' 
compile 'com.squareup.okhttp3:okhttp:3.8.0'