0

I am trying to send and recieve android notification.Lots of example use php and gcm. I try to these examples but notification send only when I enter message in index file and click the send button (enter image description here). I wanna send notification my phone to other phones which are use the same app. How can I do this ?

Büşra GÜL
  • 323
  • 1
  • 4
  • 16

1 Answers1

0

I find the solution for my problem with this code. I hope this will work for you, too.

MyFirebaseIdClass: please look at here. This class sends token id and message to gcm server.

MyFirebaseMessagingService class:

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());

}

}

please add this lines to your manifest file:

  <!-- [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] -->

finally in build gradle app file:

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'
Büşra GÜL
  • 323
  • 1
  • 4
  • 16