Skip to content

Commit 4d2c543

Browse files
committed
Initial commit
0 parents  commit 4d2c543

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1015
-0
lines changed

.gitignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/compiler.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/deploymentTargetDropDown.xml

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'com.google.gms.google-services'
4+
}
5+
6+
android {
7+
compileSdk 32
8+
9+
defaultConfig {
10+
applicationId "com.example.pushnotification"
11+
minSdk 21
12+
targetSdk 32
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
}
30+
31+
dependencies {
32+
33+
implementation 'androidx.appcompat:appcompat:1.5.1'
34+
implementation 'com.google.android.material:material:1.7.0'
35+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
36+
implementation 'com.google.firebase:firebase-messaging:23.1.1'
37+
testImplementation 'junit:junit:4.13.2'
38+
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
39+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
40+
}

app/google-services.json

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"project_info": {
3+
"project_number": "768275234449",
4+
"project_id": "pushnotification-2d871",
5+
"storage_bucket": "pushnotification-2d871.appspot.com"
6+
},
7+
"client": [
8+
{
9+
"client_info": {
10+
"mobilesdk_app_id": "1:768275234449:android:20d53ab3b5eda2ec6b0930",
11+
"android_client_info": {
12+
"package_name": "com.example.pushnotification"
13+
}
14+
},
15+
"oauth_client": [
16+
{
17+
"client_id": "768275234449-fsnntgant035ckrpmhs5jlrdbtgtutnk.apps.googleusercontent.com",
18+
"client_type": 3
19+
}
20+
],
21+
"api_key": [
22+
{
23+
"current_key": "AIzaSyBYV2ony0ZzJXahqbVUK0YlptimxHuG9EQ"
24+
}
25+
],
26+
"services": {
27+
"appinvite_service": {
28+
"other_platform_oauth_client": [
29+
{
30+
"client_id": "768275234449-fsnntgant035ckrpmhs5jlrdbtgtutnk.apps.googleusercontent.com",
31+
"client_type": 3
32+
}
33+
]
34+
}
35+
}
36+
}
37+
],
38+
"configuration_version": "1"
39+
}

app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.pushnotification;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Instrumented test, which will execute on an Android device.
15+
*
16+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
17+
*/
18+
@RunWith(AndroidJUnit4.class)
19+
public class ExampleInstrumentedTest {
20+
@Test
21+
public void useAppContext() {
22+
// Context of the app under test.
23+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24+
assertEquals("com.example.pushnotification", appContext.getPackageName());
25+
}
26+
}

app/src/main/AndroidManifest.xml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="com.example.pushnotification">
5+
6+
<application
7+
android:allowBackup="true"
8+
android:dataExtractionRules="@xml/data_extraction_rules"
9+
android:fullBackupContent="@xml/backup_rules"
10+
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
12+
android:roundIcon="@mipmap/ic_launcher_round"
13+
android:supportsRtl="true"
14+
android:theme="@style/Theme.PushNotification"
15+
tools:targetApi="31">
16+
<activity
17+
android:name=".MainActivity"
18+
android:exported="true">
19+
<intent-filter>
20+
<action android:name="android.intent.action.MAIN" />
21+
22+
<category android:name="android.intent.category.LAUNCHER" />
23+
</intent-filter>
24+
</activity>
25+
26+
<service
27+
android:name=".notification.FirebaseServices"
28+
android:exported="false"
29+
android:permission="TODO">
30+
<intent-filter>
31+
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
32+
</intent-filter>
33+
</service>
34+
</application>
35+
36+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.example.pushnotification;
2+
3+
import androidx.annotation.NonNull;
4+
import androidx.appcompat.app.AppCompatActivity;
5+
6+
import android.app.NotificationChannel;
7+
import android.app.NotificationManager;
8+
import android.os.Build;
9+
import android.os.Bundle;
10+
import android.util.Log;
11+
import android.widget.TextView;
12+
import android.widget.Toast;
13+
14+
import com.google.android.gms.tasks.OnCompleteListener;
15+
import com.google.android.gms.tasks.Task;
16+
import com.google.firebase.messaging.FirebaseMessaging;
17+
18+
public class MainActivity extends AppCompatActivity {
19+
20+
private static final String TAG = "PushNotification";
21+
private static final String CHANNEL_ID = "101";
22+
23+
24+
25+
@Override
26+
protected void onCreate(Bundle savedInstanceState) {
27+
super.onCreate(savedInstanceState);
28+
setContentView(R.layout.activity_main);
29+
30+
createNotificationChannel();
31+
getToken();
32+
}
33+
34+
private void getToken() {
35+
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
36+
@Override
37+
public void onComplete(@NonNull Task<String> task) {
38+
39+
if (!task.isSuccessful()){
40+
Log.d(TAG, "onComplete: Failed to get token");
41+
};
42+
43+
String token = task.getResult();
44+
Log.d(TAG, "onComplete: "+token);
45+
46+
}
47+
});
48+
}
49+
50+
private void createNotificationChannel() {
51+
52+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
53+
CharSequence name = "firebaseNotifChannel";
54+
String description = "Receive Firebase notification";
55+
int importance = NotificationManager.IMPORTANCE_DEFAULT;
56+
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
57+
channel.setDescription(description);
58+
NotificationManager notificationManager = getSystemService(NotificationManager.class);
59+
notificationManager.createNotificationChannel(channel);
60+
}
61+
}
62+
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.example.pushnotification.notification;
2+
3+
import android.annotation.SuppressLint;
4+
import android.app.PendingIntent;
5+
import android.content.Intent;
6+
import android.os.Build;
7+
import android.util.Log;
8+
9+
import androidx.annotation.NonNull;
10+
import androidx.annotation.RequiresApi;
11+
import androidx.core.app.NotificationCompat;
12+
import androidx.core.app.NotificationManagerCompat;
13+
14+
import com.example.pushnotification.MainActivity;
15+
import com.example.pushnotification.R;
16+
import com.google.firebase.messaging.FirebaseMessagingService;
17+
import com.google.firebase.messaging.RemoteMessage;
18+
19+
import java.util.Objects;
20+
21+
@SuppressLint("MissingFirebaseInstanceTokenRefresh")
22+
public class FirebaseServices extends FirebaseMessagingService {
23+
24+
private static final String TAG = "PushNotification";
25+
private static final String CHANNEL_ID ="101";
26+
27+
@RequiresApi(api = Build.VERSION_CODES.M)
28+
@Override
29+
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
30+
showNotification(Objects.requireNonNull(remoteMessage.getNotification()).getTitle(),remoteMessage.getNotification().getBody());
31+
}
32+
33+
@RequiresApi(api = Build.VERSION_CODES.M)
34+
private void showNotification(String title, String message) {
35+
36+
// Create an explicit intent for an Activity in your app
37+
Intent intent = new Intent(this, MainActivity.class);
38+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
39+
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
40+
41+
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
42+
.setSmallIcon(R.drawable.ic_launcher_background)
43+
.setContentTitle(title)
44+
.setContentText(message)
45+
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
46+
// Set the intent that will fire when the user taps the notification
47+
.setContentIntent(pendingIntent)
48+
.setAutoCancel(true);
49+
50+
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
51+
notificationManager.notify(1, builder.build());
52+
53+
}
54+
}

0 commit comments

Comments
 (0)