🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / Android RMOS / Firebase Cloud Messaging 2023 JAVA Example

1-) Android RMOS - Firebase Cloud Messaging 2023 JAVA Example

 

githublink : https://github.com/ramazanhaber/fcmpush2023java

mail : 07flutteregitim@gmail.com

kaynak video : https://youtu.be/crJaLpCiNfI

 

1- AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

 

<activity
    
android:name=".MainActivity"
    
android:exported="true"

 

...

...

 

<service
        
android:name=".FirebaseMsgService"
        
android:exported="true">
        <
intent-filter>
            <
action android:name="com.google.firebase.MESSAGING_EVENT" />
        </
intent-filter>
    </
service>
</
application>

 

 

 

 

2- FirebaseMsgService.java

 

package com.ramzey.fcmpush2023.FCM;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.ramzey.fcmpush2023.MainActivity;
import com.ramzey.fcmpush2023.R;

public class FirebaseMsgService extends FirebaseMessagingService {
    
@Override
    
public void onNewToken(@NonNull String token) {
        
super.onNewToken(token);
        
Log.d("RefreshToken", token);
    }

    
@Override
    
public void onMessageReceived(@NonNull RemoteMessage message) {
        
super.onMessageReceived(message);
        
RemoteMessage.Notification noti = message.getNotification();
        
if (noti != null) {
            pushNotification(
noti.getTitle(), noti.getBody());
        }
    }

    
public void pushNotification(String title, String body) {
        
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        
Notification notification;

        
String channelId = "default";
        
Intent intent = new Intent(this, MainActivity.class);

        
Intent iNotify = new Intent(this, MainActivity.class);
        
iNotify.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        PendingIntent pi = PendingIntent.getActivity(this, 100, iNotify, PendingIntent.FLAG_UPDATE_CURRENT);


        
PendingIntent pendingIntent;
        
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            
pendingIntent = PendingIntent.getActivity(this,
                    
0, new Intent(this, getClass()).addFlags(
                            
Intent.FLAG_ACTIVITY_SINGLE_TOP),
                    
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
        }
else {
            
pendingIntent = PendingIntent.getActivity(this,
                    
0, new Intent(this, getClass()).addFlags(
                            
Intent.FLAG_ACTIVITY_SINGLE_TOP),
                    
PendingIntent.FLAG_UPDATE_CURRENT);
        }


        
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            
CharSequence name = "default";
            
String description = "Channel for Push Notifications";
            
int importance = NotificationManager.IMPORTANCE_DEFAULT;
            
NotificationChannel channel = new NotificationChannel(channelId, name, importance);
            
channel.setDescription(description);
            
if (notificationManager != null) {
                
notificationManager.createNotificationChannel(channel);
            }

            
notification = new Notification.Builder(this)
                    .setSmallIcon(
R.drawable.a)
                    .setContentIntent(
pendingIntent)
                    .setContentTitle(title)
                    .setSubText(body)
                    .setAutoCancel(
true)
                    .setChannelId(
channelId)
                    .build();
        }
else {
            
notification = new Notification.Builder(this)
                    .setSmallIcon(
R.drawable.a)
                    .setContentIntent(
pendingIntent)
                    .setAutoCancel(
true)
                    .setContentTitle(title)
                    .setSubText(body)
                    .build();
        }
        
if (notificationManager != null) {
            
notificationManager.notify(1, notification);
        }

    }
}

 

 

 

3- MainActivity.java

 

@Override
protected void onCreate(Bundle savedInstanceState) {
    
super.onCreate(savedInstanceState);

 

---

---

Log.d("Token : ","aa");
    
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
        
@Override
        
public void onComplete(@NonNull Task<String> task) {
            
if(!task.isSuccessful()){
                
Log.d("TokenError","Token Failed");
                
return;
            }

            
String token  = task.getResult();
            
Log.d("Token : ",token);
        }
    });
}

 

 

 

4- build.gradle (project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    
dependencies {
        
classpath 'com.google.gms:google-services:4.3.15'
    
}
}
plugins {
    
id 'com.android.application' version '7.2.0' apply false
    
id 'com.android.library' version '7.2.0' apply false
}

task clean(type: Delete) {
    
delete rootProject.buildDir
}

 

 

5- build.gradle (module)

/plugins {
    
id 'com.android.application'
    
id 'com.google.gms.google-services'
}

android {
    
compileSdk 33

    
defaultConfig {
        
applicationId "com.ramzey.fcmpush2023"
        
minSdk 21
        
targetSdk 33
        
versionCode 1
        
versionName "1.0"

        
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    
}

    
buildTypes {
        
release {
            
minifyEnabled false
            
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        
}
    }
    
compileOptions {
        
sourceCompatibility JavaVersion.VERSION_1_8
        
targetCompatibility JavaVersion.VERSION_1_8
    
}
    
buildFeatures {
        
viewBinding true
    
}
}

dependencies {

    
implementation 'androidx.appcompat:appcompat:1.6.1'
    
implementation 'com.google.android.material:material:1.8.0'
    
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    
implementation 'androidx.navigation:navigation-fragment:2.5.3'
    
implementation 'androidx.navigation:navigation-ui:2.5.3'
    
implementation 'com.google.firebase:firebase-messaging:23.1.1'
    
implementation platform('com.google.firebase:firebase-bom:31.2.2')
    implementation
'com.google.firebase:firebase-analytics'
    
implementation 'androidx.work:work-runtime:2.7.1'

    
testImplementation 'junit:junit:4.13.2'
    
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

 

 

6- settings.gradle

/pluginManagement {
    
repositories {
        
gradlePluginPortal()
        google()
        mavenCentral()
    
}
}
dependencyResolutionManagement {
    
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories
{
        
google()
        mavenCentral()
    
}
}
rootProject.name = "fcmpush2023"
include ':app'

 

 

 

7- build.gradle (module)

/plugins {
    
id 'com.android.application'
    
id 'com.google.gms.google-services'
}

android {
    
compileSdk 33

    
defaultConfig {
        
applicationId "com.ramzey.fcmpush2023"
        
minSdk 21
        
targetSdk 33
        
versionCode 1
        
versionName "1.0"

        
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    
}

    
buildTypes {
        
release {
            
minifyEnabled false
            
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        
}
    }
    
compileOptions {
        
sourceCompatibility JavaVersion.VERSION_1_8
        
targetCompatibility JavaVersion.VERSION_1_8
    
}
    
buildFeatures {
        
viewBinding true
    
}
}

dependencies {

    
implementation 'androidx.appcompat:appcompat:1.6.1'
    
implementation 'com.google.android.material:material:1.8.0'
    
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    
implementation 'androidx.navigation:navigation-fragment:2.5.3'
    
implementation 'androidx.navigation:navigation-ui:2.5.3'
    
implementation 'com.google.firebase:firebase-messaging:23.1.1'
    
implementation platform('com.google.firebase:firebase-bom:31.2.2')
    implementation
'com.google.firebase:firebase-analytics'
    
implementation 'androidx.work:work-runtime:2.7.1'

    
testImplementation 'junit:junit:4.13.2'
    
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

 

 

8- gradle.properties

org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8

android.useAndroidX=true
android.nonTransitiveRClass=true

 

 

 

 POSTMAN ile göndermek

EKRAN 1

 

EKRAN 2

 

 

 

https://fcm.googleapis.com/fcm/send

Authorization  

key=AAAAAIVOT7A:APaaaaaaaaaaaaaaaaa4h_iAHs9Wv1o-9VHazXzHbl9ABkaaaaaaaaaNPOiBC3HUEeVvQf7lSPPqB-sn3VewwwwwwwwwwwGnswx-uj00AAyqBrMaaaaaaaaaaaZV278Cp3Ad5_8BWSD

 

 

 

{

   "to":"fxZxcQKdRpmXi6hnhdVhoa:APA91bHHODbofmMcaxF3qXcc1F7UBGQBuo315lylsLM2PgSKxDIq5yWdMkjM7959zO_Fv25HMK1KNVnDPodNK8sUjXMs3gaFQaPYifKfGdO3Fvvu6KNYdug_2-LjO0nM-RDGXFcfb03O",

   "notification":{

      "body":"frw",

      "title":"ODALAR -> 212 -> ELEKTRIK -> LAMBA"

   }

}

 

 

 

 

 

 

 

 

 

HATA : Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified

 

ÇÖMÜMÜ KAYNAK : https://stackoverflow.com/questions/70894168/targeting-s-version-31-and-above-requires-that-one-of-flag-immutable-or-flag

 

 

AYRICA --> multiDexEnabled true -> yapabilirsin asagıdaki gibi

 

defaultConfig {
    
applicationId "com.ramazan.user.rmospersonel"
    
minSdkVersion 19
    
targetSdkVersion 33
    
versionCode 32
    
versionName "32.0"
    
multiDexEnabled true
}

 

 2023 Şubat 22 Çarşamba
 293