Skip to content

Commit

Permalink
🔔 Enable Push Notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
adithya321 committed Mar 5, 2017
1 parent 3cb3fe4 commit f18ad1f
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ dependencies {
compile 'com.android.support:support-v4:25.2.0'

compile 'com.google.firebase:firebase-auth:10.2.0'
compile 'com.google.firebase:firebase-core:10.2.0'
compile 'com.google.firebase:firebase-database:10.2.0'
compile 'com.google.firebase:firebase-messaging:10.2.0'
compile 'com.firebaseui:firebase-ui-database:1.2.0'

compile 'com.google.android.gms:play-services-maps:10.2.0'
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,18 @@
<activity
android:name=".activities.GalleryViewPagerActivity"
android:theme="@style/EventsTheme" />

<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Instincts
* Copyright (C) 2017 Adithya J
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/

package com.pimp.instincts;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.pimp.instincts.activities.HomeActivity;

import java.util.Map;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

// Create Notification when push message received in foreground
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Log.e("FCM", "Message data payload: " + remoteMessage.getData());
}

Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("google.sent_time", remoteMessage.getSentTime());
intent.putExtra("from", remoteMessage.getFrom());
intent.putExtra("google.message_id", remoteMessage.getMessageId());
intent.putExtra("collapse_key", remoteMessage.getCollapseKey());
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
intent.putExtra(entry.getKey(), entry.getValue());
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setColor(Color.parseColor(remoteMessage.getData().get("color")))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(remoteMessage.getData().get("content")))
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("content"))
.setTicker(remoteMessage.getData().get("content"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

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

notificationManager.notify(0, notificationBuilder.build());
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f18ad1f

Please sign in to comment.