Created
April 14, 2017 23:33
-
-
Save mitchtabian/db59834f17b689d8076e7eb544c89beb to your computer and use it in GitHub Desktop.
FirebaseOnClickPushNotification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/activity_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="com.tabian.firebasepushnotifications.MainActivity"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Hello World!"/> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.tabian.firebasepushnotifications"> | |
<uses-permission android:name="android.permission.INTERNET"></uses-permission> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN"/> | |
<category android:name="android.intent.category.LAUNCHER"/> | |
</intent-filter> | |
<intent-filter> | |
<action android:name="MAINACTIVITY"></action> | |
<category android:name="android.intent.category.DEFAULT"></category> | |
</intent-filter> | |
</activity> | |
<activity android:name=".SomeActivity"> | |
<intent-filter> | |
<action android:name="SOMEACTIVITY"></action> | |
<category android:name="android.intent.category.DEFAULT"></category> | |
</intent-filter> | |
</activity> | |
<service | |
android:name=".FirebaseMessagingService"> | |
<intent-filter> | |
<action android:name="com.google.firebase.MESSAGING_EVENT"/> | |
</intent-filter> | |
</service> | |
<meta-data | |
android:name="com.google.firebase.messaging.default_notification_color" | |
android:resource="@color/colorAccent" /> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"to": | |
"/topics/NEWS" | |
, | |
"data": { | |
"extra_information": "This is some extra information" | |
}, | |
"notification": { | |
"title": "NEW NOTIFICATION!", | |
"text": "Click me to open an Activity!", | |
"click_action": "SOMEACTIVITY" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tabian.firebasepushnotifications; | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.media.RingtoneManager; | |
import android.net.Uri; | |
import android.support.v4.app.NotificationCompat; | |
import android.util.Log; | |
import com.google.firebase.messaging.RemoteMessage; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
/** | |
* Created by User on 2/20/2017. | |
*/ | |
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService { | |
private static final String TAG = "FirebaseMessagingServic"; | |
public FirebaseMessagingService() { | |
} | |
@Override | |
public void onMessageReceived(RemoteMessage remoteMessage) { | |
// Check if message contains a data payload. | |
if (remoteMessage.getData().size() > 0) { | |
Log.d(TAG, "Message data payload: " + remoteMessage.getData()); | |
try { | |
JSONObject data = new JSONObject(remoteMessage.getData()); | |
String jsonMessage = data.getString("extra_information"); | |
Log.d(TAG, "onMessageReceived: \n" + | |
"Extra Information: " + jsonMessage); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
// Check if message contains a notification payload. | |
if (remoteMessage.getNotification() != null) { | |
String title = remoteMessage.getNotification().getTitle(); //get title | |
String message = remoteMessage.getNotification().getBody(); //get message | |
String click_action = remoteMessage.getNotification().getClickAction(); //get click_action | |
Log.d(TAG, "Message Notification Title: " + title); | |
Log.d(TAG, "Message Notification Body: " + message); | |
Log.d(TAG, "Message Notification click_action: " + click_action); | |
sendNotification(title, message,click_action); | |
} | |
} | |
@Override | |
public void onDeletedMessages() { | |
} | |
private void sendNotification(String title,String messageBody, String click_action) { | |
Intent intent; | |
if(click_action.equals("SOMEACTIVITY")){ | |
intent = new Intent(this, SomeActivity.class); | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
} | |
else if(click_action.equals("MAINACTIVITY")){ | |
intent = new Intent(this, MainActivity.class); | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
}else{ | |
intent = new Intent(this, MainActivity.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(title) | |
.setContentText(messageBody) | |
.setAutoCancel(true) | |
.setSound(defaultSoundUri) | |
.setContentIntent(pendingIntent); | |
NotificationManager notificationManager = | |
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tabian.firebasepushnotifications; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import com.google.firebase.messaging.FirebaseMessaging; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
FirebaseMessaging.getInstance().subscribeToTopic("NEWS"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:textSize="25sp" | |
android:layout_marginTop="50dp" | |
android:text="You Opened the Notification!" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentTop="true" | |
android:layout_centerHorizontal="true" | |
android:id="@+id/textOpenedNotification" | |
/> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tabian.firebasepushnotifications; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v7.app.AppCompatActivity; | |
/** | |
* Created by User on 2/23/2017. | |
*/ | |
public class SomeActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.some_activity_layout); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment