I have a little problem: If I send a notification to my android device via php from the web, a notification gets built, but isn't vibrating outside the app. It is only vibrating while I am inside the app.
Here are my codes:
PHP:
function send_notification ($token, $message, $Raum, $Notruf){
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array('to' => $token, 'notification' => array('body' => "Raum ".$Raum.", Notruf ".$Notruf), 'data' => array ('message' => $message));
$headers = array ('Authorization:key = (HERE IS MY AUTH KEY)','Content-Type: application/json');
$ch = curl_init ();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result == FALSE) {
die ('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$sql = 'SELECT token FROM devices as d, Sani as s WHERE d.sean = s.sean AND s.Status!=0';
$result = mysqli_query($db, $sql);
$tokens = array ();
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)){
$tokens[] = $row["token"];
}
}
mysqli_close($db);
$message = array ("message" => "Raum ".$Raum.", Notruf ".$Notruf);
for ($i = 0; $i < count($tokens); ++$i) {
$message_status = send_notification ($tokens[$i], $message, $Raum, $Notruf);
echo $message_status;
}
My Android Notification Code:
public class MyNotificationManager {
private Context mCtx;
private static MyNotificationManager mInstance;
private MyNotificationManager(Context context){
mCtx=context;
}
public static synchronized MyNotificationManager getInstance(Context context){
if(mInstance== null){
mInstance = new MyNotificationManager(context);
}
return mInstance;
}
public void displayNotification(String title, String body){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx, Constants.CHANNEL_ID)
.setSmallIcon(R.drawable.sirenlight)
.setContentTitle(title)
.setVibrate(new long[] {0, 5000, 100, 5000, 100, 5000, 100, 5000, 100, 5000, 100, 5000})
.setContentText(body);
Intent intent = new Intent(mCtx, EinsatzInterface.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
if(mNotificationManager!=null){
mNotificationManager.notify(1, mBuilder.build());
}
}
}
I couldn't get any fixes online, so I am asking here. How I said, the vibration is working, but only inside the app. If I close it, even without stopping the process, it only shows the notification and doesn't vibrate or show the correct logo.
When inside the app the notification looks like this (vibrating and icon showing):
When outside the app the notification looks like this (not vibrating and icon not showing):