Last active
January 15, 2020 08:44
-
-
Save numanayhan/494809eadf28b7729276b4f950cba449 to your computer and use it in GitHub Desktop.
NotificationService
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.panda.notifications"> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
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> | |
</activity> | |
<service | |
android:name=".NotificationService" | |
android:exported="false"> | |
<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_panda_notify" /> | |
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming | |
notification message. See README(https://goo.gl/6BKBk7) for more. --> | |
<meta-data | |
android:name="com.google.firebase.messaging.default_notification_color" | |
android:resource="@color/colorPrimary" /> | |
</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
public class NotificationService extends FirebaseMessagingService { | |
private static final String TAG = "FirebaseService"; | |
private static int count = 0; | |
@RequiresApi(api = Build.VERSION_CODES.O) | |
@Override | |
public void onMessageReceived(RemoteMessage remoteMessage) { | |
String click_action = remoteMessage.getNotification().getClickAction(); | |
if (remoteMessage.getNotification() != null) { | |
sendNotification(remoteMessage.getNotification().getTitle(), | |
remoteMessage.getNotification().getBody(), remoteMessage.getData(),click_action); | |
} | |
} | |
@RequiresApi(api = Build.VERSION_CODES.O) | |
public void sendNotification(String messageTitle, String messageBody, Map<String, String> row, String click_action) { | |
String CHANNEL_ID = "panda"; | |
CharSequence name = "panda"; | |
Uri NOTIFICATION_SOUND_URI = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + BuildConfig.APPLICATION_ID + "/" + R.raw.voice_notification); | |
long[] VIBRATE_PATTERN = {0, 500}; | |
int importance = NotificationManager.IMPORTANCE_HIGH; | |
Intent resultIntent = new Intent(click_action); | |
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
Bundle bundle = new Bundle(); | |
resultIntent.putExtras(bundle); | |
resultIntent.putExtra("notify","true"); | |
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance); | |
mChannel.setShowBadge(true); | |
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); | |
stackBuilder.addParentStack(MainActivity.class); | |
stackBuilder.addNextIntent(resultIntent); | |
PendingIntent pendingIntent = (PendingIntent) PendingIntent.getActivity(getApplicationContext(),0,resultIntent, | |
PendingIntent.FLAG_UPDATE_CURRENT); | |
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); | |
Notification notificationBuilder = new Notification.Builder(this) | |
.setSmallIcon(R.drawable.ic_panda_notify) | |
.setColor(getColor(R.color.colorPrimary)) | |
.setContentTitle(messageTitle) | |
.setContentText(messageBody) | |
.setVibrate(VIBRATE_PATTERN) | |
.setAutoCancel(true) | |
.setStyle(new Notification.BigTextStyle()) | |
.setContentIntent(pendingIntent).build(); | |
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
notificationManager.notify(count, notificationBuilder); | |
count++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment