【问题标题】:Send Notification phone to phone发送通知电话到电话
【发布时间】:2017-05-03 11:47:23
【问题描述】:

我正在尝试发送和接收 android 通知。很多示例使用 php 和 gcm。我尝试使用这些示例,但仅当我在索引文件中输入消息并单击发送按钮时才会发送通知 (enter image description here)。我想向我的手机发送通知到使用相同应用的其他手机。我怎样才能做到这一点 ?

【问题讨论】:

标签: android push-notification notifications


【解决方案1】:

我找到了解决此代码问题的方法。我希望这对你也有用。

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'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多