Created
September 28, 2021 14:22
-
-
Save codinginflow/ea1bac575ed094d95153dbdfe14712d3 to your computer and use it in GitHub Desktop.
Notifications Tutorial Part 3
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"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:padding="16dp" | |
tools:context="com.codinginflow.notificationsexample.MainActivity"> | |
<EditText | |
android:id="@+id/edit_text_title" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Title" /> | |
<EditText | |
android:id="@+id/edit_text_message" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Message" /> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:onClick="sendOnChannel1" | |
android:text="Send on Channel 1" /> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:onClick="sendOnChannel2" | |
android:text="Send on Channel 2" /> | |
</LinearLayout> |
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.codinginflow.notificationsexample"> | |
<application | |
android:name=".App" | |
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> | |
<receiver android:name=".NotificationReceiver" /> | |
</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
package com.codinginflow.notificationsexample; | |
import android.app.Application; | |
import android.app.NotificationChannel; | |
import android.app.NotificationManager; | |
import android.os.Build; | |
public class App extends Application { | |
public static final String CHANNEL_1_ID = "channel1"; | |
public static final String CHANNEL_2_ID = "channel2"; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
createNotificationChannels(); | |
} | |
private void createNotificationChannels() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
NotificationChannel channel1 = new NotificationChannel( | |
CHANNEL_1_ID, | |
"Channel 1", | |
NotificationManager.IMPORTANCE_HIGH | |
); | |
channel1.setDescription("This is Channel 1"); | |
NotificationChannel channel2 = new NotificationChannel( | |
CHANNEL_2_ID, | |
"Channel 2", | |
NotificationManager.IMPORTANCE_LOW | |
); | |
channel2.setDescription("This is Channel 2"); | |
NotificationManager manager = getSystemService(NotificationManager.class); | |
manager.createNotificationChannel(channel1); | |
manager.createNotificationChannel(channel2); | |
} | |
} | |
} |
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.codinginflow.notificationsexample; | |
import android.app.Notification; | |
import android.app.PendingIntent; | |
import android.content.Intent; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Color; | |
import android.support.v4.app.NotificationCompat; | |
import android.support.v4.app.NotificationManagerCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.EditText; | |
import static com.codinginflow.notificationsexample.App.CHANNEL_1_ID; | |
import static com.codinginflow.notificationsexample.App.CHANNEL_2_ID; | |
public class MainActivity extends AppCompatActivity { | |
private NotificationManagerCompat notificationManager; | |
private EditText editTextTitle; | |
private EditText editTextMessage; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
notificationManager = NotificationManagerCompat.from(this); | |
editTextTitle = findViewById(R.id.edit_text_title); | |
editTextMessage = findViewById(R.id.edit_text_message); | |
} | |
public void sendOnChannel1(View v) { | |
String title = editTextTitle.getText().toString(); | |
String message = editTextMessage.getText().toString(); | |
Intent activityIntent = new Intent(this, MainActivity.class); | |
PendingIntent contentIntent = PendingIntent.getActivity(this, | |
0, activityIntent, 0); | |
Intent broadcastIntent = new Intent(this, NotificationReceiver.class); | |
broadcastIntent.putExtra("toastMessage", message); | |
PendingIntent actionIntent = PendingIntent.getBroadcast(this, | |
0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT); | |
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.lotti); | |
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID) | |
.setSmallIcon(R.drawable.ic_one) | |
.setContentTitle(title) | |
.setContentText(message) | |
.setLargeIcon(largeIcon) | |
.setStyle(new NotificationCompat.BigTextStyle() | |
.bigText(getString(R.string.long_dummy_text)) | |
.setBigContentTitle("Big Content Title") | |
.setSummaryText("Summary Text")) | |
.setPriority(NotificationCompat.PRIORITY_HIGH) | |
.setCategory(NotificationCompat.CATEGORY_MESSAGE) | |
.setColor(Color.BLUE) | |
.setContentIntent(contentIntent) | |
.setAutoCancel(true) | |
.setOnlyAlertOnce(true) | |
.addAction(R.mipmap.ic_launcher, "Toast", actionIntent) | |
.build(); | |
notificationManager.notify(1, notification); | |
} | |
public void sendOnChannel2(View v) { | |
String title = editTextTitle.getText().toString(); | |
String message = editTextMessage.getText().toString(); | |
Notification notification = new NotificationCompat.Builder(this, CHANNEL_2_ID) | |
.setSmallIcon(R.drawable.ic_two) | |
.setContentTitle(title) | |
.setContentText(message) | |
.setStyle(new NotificationCompat.InboxStyle() | |
.addLine("This is line 1") | |
.addLine("This is line 2") | |
.addLine("This is line 3") | |
.addLine("This is line 4") | |
.addLine("This is line 5") | |
.addLine("This is line 6") | |
.addLine("This is line 7") | |
.setBigContentTitle("Big Content Title") | |
.setSummaryText("Summary Text")) | |
.setPriority(NotificationCompat.PRIORITY_LOW) | |
.build(); | |
notificationManager.notify(2, notification); | |
} | |
} |
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.codinginflow.notificationsexample; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.widget.Toast; | |
public class NotificationReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
String message = intent.getStringExtra("toastMessage"); | |
Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); | |
} | |
} |
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
<resources> | |
<string name="app_name">Notifications Example</string> | |
<string name="long_dummy_text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc sed leo consectetur, iaculis felis non, porta urna. Integer vulputate nisl at massa facilisis fringilla. Ut egestas, eros placerat placerat accumsan, orci velit finibus sem, in placerat lorem dolor in odio. Mauris auctor tortor eget quam facilisis commodo. Nulla in auctor justo, dapibus pharetra elit. Nam malesuada gravida porttitor. Integer placerat, est condimentum facilisis commodo, magna magna sodales dolor, non vestibulum risus tellus ut nibh. Donec accumsan mauris quis libero ullamcorper, sed tincidunt purus lobortis. Nunc sed leo vel est ullamcorper aliquet id eget dolor. Maecenas imperdiet lorem pellentesque, finibus risus in, mollis dui. Cras bibendum risus id rutrum sagittis. Donec ullamcorper, nisi vel rhoncus ullamcorper, urna ligula pretium nunc, at volutpat ex erat bibendum metus.</string> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment