2017-05-03 11 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-私はこのリンクを試しましたが、ランタイムエラーが発生します:** ".._ app V/FA:非アクティブ、サービスから切断しています"。** **使用して通知を使用してfcm-without-using-xmpp-or- any –

+0

@AKSHAYMANAGOOLI –

答えて

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アプリファイル内:

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' 
関連する問題